David19801
David19801

Reputation: 11448

php from string to echo

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...

Upvotes: 2

Views: 101

Answers (3)

stealthyninja
stealthyninja

Reputation: 10371

@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];

Upvotes: 0

Brian
Brian

Reputation: 8616

$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.

Upvotes: 0

shamittomar
shamittomar

Reputation: 46692

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.

Upvotes: 1

Related Questions