How to retrieve string between two delimeters in PHP

Suppose I want to retrieve string between two delimiters of a string. For example:
SAMSUNG GALAXY CORE PRIME 4G (WHITE, 8 GB)

I want output as 'WHITE, 8 GB'

How to achieve it:

Lets follow these steps:

Add following function is your php page:
Step 1:
function getStringAfter($str,$from,$to)
{
   $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
   return substr($sub,0,strpos($sub,$to));

Step 2:
Create a variable storing string.
$title = 'SAMSUNG GALAXY CORE PRIME 4G (WHITE, 8 GB)';

Step 3:
Pass this variable in above mentioned function. There are three parameter passing in this function.
1. $str : This is string. Here $title is using for this.
2. $from: In this example, '(' is 1st delimeter.
3. $to: In this example, ')' is 2nd delimeter.

Step 4:

Now this is final step.
$result = getStringAfter($title,'(',')');

It returns here output as "WHITE, 8 GB"

Please try this with other examples and if you face any problem, comment here.
If you like, then share it on your social network.
Thanks

0 Comments
Disqus
Fb Comments
Comments :

0 comments:

Post a Comment