[PHP] i need regex help!

so i'm trying to fetch everything within <php> and </php> tags in my string, and return each instance of a match. currently my string has two sets of <php> tags. the return i'm getting is everything between the first opening tag and the last opening tag. but that's not what i need.

here be my code:
PHP:
$pattern = "/<php>(.*)<\/php>/i";
$str = "display <php>this</php> and <php>that</php>, okay?";
$match = preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER);
print_r($matches);

here's the return:
Array
(
[0] => Array
(
[0] => <php>this</php> and <php>that</php>
)

[1] => Array
(
[0] => this</php> and <php>that
)

)

ideally i'd like to have in my $matches array the values this and that

i'm not familiar with PHP's regex functions, and even less familiar with regex patterns. any help would be great, thanks.
 
You need to use a non-greedy regular expression. By default, a regex grabs as much data as it can to match your expression. Since you have a .*, it will grab from the first php tag to the last since the most it can match is everything in between. If you change .* to .*? it will be a non-greedy match.

PHP:
$pattern = "/<php>(.*?)<\/php>/i";
$str = "display <php>this</php> and <php>that</php>, okay?";
$match = preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER);
print_r($matches);
 
heres your regular expression
bush_finger_flip.jpg


Spoiler
 
Back
Top