Quote:
Originally Posted by mgriggs13
I have researched the jist behind making this work but I am having a unique problem that I can't seem to fix.
I use the following code to grab the text between these two tags.
-
$content = "[tag]Hello[/tag]";
-
preg_match_all("/(\[([\w]+)\])(.*)(\[\/\\2\])/", $content, $matches);
-
print_r($matches);
-
-
This yields the following results succesfully:
-
Array
-
(
-
[0] => Array
-
(
-
[0] => [tag]Hello[/tag]
-
)
-
[1] => Array
-
(
-
[0] => [tag]
-
)
-
[2] => Array
-
(
-
[0] => tag
-
)
-
[3] => Array
-
(
-
[0] => Hello
-
)
-
[4] => Array
-
(
-
[0] => [/tag]
-
)
-
)
-
The problem arises when there are duplicate tags.
-
$content = "[tag]Hello[/tag] More Text [tag]Hello2[/tag]";
-
preg_match_all("/(\[([\w]+)\])(.*)(\[\/\\2\])/", $content, $matches);
-
print_r($matches);
-
-
With this I end up with the following:
-
Array
-
(
-
[0] => Array
-
(
-
[0] => [tag]Hello[/tag] More Text [tag]Hello2[/tag]
-
)
-
[1] => Array
-
(
-
[0] => [tag]
-
)
-
[2] => Array
-
(
-
[0] => tag
-
)
-
[3] => Array
-
(
-
[0] => Hello[/tag] More Text [tag]Hello2
-
)
-
[4] => Array
-
(
-
[0] => [/tag]
-
)
-
)
-
-
I need to be able to have it stop at the first close tag and then register the second open and close tag in a different array. Any Ideas?
What is happening is that PHP is matching as many characters as it can for the quantifier .* in your regular expression.
To make the quantifiers match the least amount of characters use the "un-greedy" indicator, "?".
eg:
[PHP]preg_match_all("/(\[([\w]+)\])(.*?)(\[\/\\2\])/", $content, $matches);[/PHP]
notice you now have (.*?) matching the characters in between tags rather than the previous (.*)
(.*?) will match until it reaches the first (\[\/\\2\])
before you had:
(.*) will match until it reaches the last (\[\/\\2\])