For sure this has already been asked by someone else, however I\'ve searched here on SO and found nothing https://stackoverflow.com/search?q=php+parse+between+words
I ha
You can also use two explode statements.
For example, say you want to get "z" in y=mx^z+b. To get z:
$formula="y=mx^z+b";
$z=explode("+",explode("^",$formula)[1])[0];
First I get everything after ^: explode("^",$formula)[1]
Then I get everything before +: explode("+",$previousExplode)[0]
I'm not much familiar with PHP, but it seems to me that you can use something like:
if (preg_match("/(?<=First).*?(?=Second)/s", $haystack, $result))
print_r($result[0]);
(?<=First)
looks behind for First
but doesn't consume it,
.*?
Captures everything in between First
and Second
,
(?=Second)
looks ahead for Second
but doesn't consume it,
The s
at the end is to make the dot .
match newlines if any.
To get all the text between those delimiters, you use preg_match_all
and you can use a loop to get each element:
if (preg_match_all("/(?<=First)(.*?)(?=Second)/s", $haystack, $result))
for ($i = 1; count($result) > $i; $i++) {
print_r($result[$i]);
}
This allows you to run the same function with different parameters, just so you don't have to rewrite this bit of code all of the time. Also uses the strpos which you used. Has been working great for me.
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);
}
$fullstring = 'This is a long set of words that I am going to use.';
$parsed = get_string_between($fullstring, 'This', "use");
echo $parsed;
Will output:
is a long set of words that I am going to
Not sure that the result will be faster than your code, but you can do it like this with regex:
$pattern = '~(?<=' . preg_quote($start, '~')
. ').+?(?=' . preg_quote($end, '~') . ')~si';
if (preg_match($pattern, $subject, $match))
print_r($match[0]);
I use preg_quote to escape all characters that have a special meaning in a regex (like +*|()[]{}.?
and the pattern delimiter ~
)
(?<=..)
is a lookbehind assertion that check a substring before what you want to find.
(?=..)
is a lookahead assertion (same thing for after)
.+?
means all characters one or more times but the less possible (the question mark make the quantifier lazy)
s
allows the dot to match newlines (not the default behavior)
i
make the search case insensitive (you can remove it, if you don't need)
Here's a simple example for finding everything between the words 'mega' and 'yo' for the string $t
.
PHP Example
$t = "I am super mega awesome-sauce, yo!";
$arr = [];
preg_match("/mega\ (.*?)\ yo/ims", $t, $arr);
echo $arr[1];
PHP Output
awesome-sauce,