Reputation: 257
I'm using this regex to pull out sections from content submitted by a user:
preg_match_all("~\[section name=[\"|'](.*?)[\"|']\](.*?)\[\/section\]~", $content, $matches, PREG_SET_ORDER);
The purpose is to allow non-technical users to easily create different named sections. As far as I can tell, PHP's bbcode parser won't allow me to pull out contents and attribute values into an array in this way.
I'm not sure why the accepted answer at preg_match_all parsing, only one match was accepted, because it suggests that preg_match_all doesn't globally match by default, and the suggested g
flag seems to be invalid.
The current function correctly matches so that:
[section name="one"]this is section one[/section]
Gets put into $matches
:
array (size=1)
0 =>
array (size=3)
0 => string '[section name="one"]this is section one[/section]'
1 => string 'one'
2 => string 'this is section one'
This is the desired behavior.
However, it doesn't add further matches to the $matches
array, only the first.
What do I need to do to have $matches
populated with all the matches in the given string?
Upvotes: 1
Views: 191
Reputation: 4854
It is strange, because when I run the script:
$content = '[section name="one"]this is section one[/section]<br />[section name="two"]this is section two[/section]';
preg_match_all("~\[section name=[\"|'](.*?)[\"|']\](.*?)\[\/section\]~", $content, $matches, PREG_SET_ORDER);
print_r($matches);
It prints:
Array
(
[0] => Array
(
[0] => [section name="one"]this is section one[/section]
[1] => one
[2] => this is section one
)
[1] => Array
(
[0] => [section name="two"]this is section two[/section]
[1] => two
[2] => this is section two
)
)
Upvotes: 1