I'm trying to get from a text all the occurrences of a code snippet and the 3 parameters. I do this using a regular expression and preg_match_all PHP function.
It works fine if I have just one occurrence of the snippet present in the text. If there are two or more I get a weird result.
I'm not so expert with regular expressions so I have some difficulties to understand what am I missing.
Function
public function getGallerySnippetOccurrences($text) {
$ptn = '/{# +gallery +(src|width|height)=\[(.*)\] +(src|width|height)=\[(.*)\] +(src|width|height)=\[(.*)\] +#}/';
if(preg_match_all($ptn,$text,$matches)){
$turnedMatches = $this->turn_array($matches);
return $turnedMatches;
}
else {
return null;
}
}
Text 1 (in this case works as aspected)
Lorem ipsum {# gallery src=[holiday_images/london] width=[400] height=[300] #} sid amet.
Returns:
array(1) {
[0] =>
array(7) {
[0] =>
string(66) "{# gallery src=[holiday_images/london] width=[400] height=[300] #}"
[1] =>
string(3) "src"
[2] =>
string(21) "holiday_images/london"
[3] =>
string(5) "width"
[4] =>
string(3) "400"
[5] =>
string(6) "height"
[6] =>
string(3) "300"
}
}
Text 2 (unespected behaviour)
Lorem ipsum {# gallery src=[holiday_images/london] width=[400] height=[300] #} sid amet {# gallery src=[holiday_images/paris] width=[400] height=[300] #}
Returns
array(1) {
[0] =>
array(7) {
[0] =>
string(141) "{# gallery src=[holiday_images/london] width=[400] height=[300] #} sid amet {# gallery src=[holiday_images/paris] width=[400] height=[300] #}"
[1] =>
string(3) "src"
[2] =>
string(96) "holiday_images/london] width=[400] height=[300] #} sid amet {# gallery src=[holiday_images/paris"
[3] =>
string(5) "width"
[4] =>
string(3) "400"
[5] =>
string(6) "height"
[6] =>
string(3) "300"
}
}
What am I doing wrong?
/{# +gallery +(src|width|height)=\[(.*?)] +(src|width|height)=\[(.*?)] +(src|width|height)=\[(.*?)] +#}/