2

I have a "big" string like something here blah bla <b>tomato</b> something else kasjd ajsd

From this I am trying to extract tomato.

What is the function to use to do this based on getting the position in the big string of the strings to the left and right of the target string?

so like I have:

start: "bla <b>"
end: "</b> some"

and I want what is between them in the big string...

3 Answers 3

1

Use this function:

function get_string_between($string, $start, $end)
{
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0)
        return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

echo get_string_between(
     'something here blah bla <b>tomato</b> something else kasjd ajsd',
      'bla <b>',
      '</b> some'
);

See it working here on codepad.

Sign up to request clarification or add additional context in comments.

Comments

0
$things = split("<[/]?b>", $string);

This will return an array of anything inbetween xxx i.e if there are multiple 's in your string will give you them.

Comments

0

@David19801: If the "keyword" you're trying to extract will always be the same and is known, why not

$pieces = explode("tomato", $bigstring);
$left   = $pieces[0];
$right  = $pieces[1];

1 Comment

Sometimes it is a number that can change frequently. I just know what is to its right and left...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.