Regex PHP to find BBCode tag contents
I have a string such as this:
Land of gray [example]here is an example[/example] and pink.
I'm struggling to get the PHP/regex code that will return the contents of the [example]
tag into a variabl开发者_JAVA技巧e...
I'm not a PHP expert but... this regex will work
\[example\]([^\[]*)\[
That will capture the contents in the capture group.
So your example contents should be in $matches[1] ???
ex:
<?php
$subject = "Land of gray [example]here is an example[/example] and pink.";
$pattern = '/\[example\]([^\[]*)\[/';
preg_match($pattern, $subject, $matches);
print_r($matches[1]);
?>
I didn't test the code above because I don't have PHP running on this machine but I think that would work...
BB code allows attributes. [url="somelink"]click[/url] will not be parsed properly with this regex. I would like to give you an answer, but my regex does'nt work well and it's the reason I got here in the first place. Lol. ;-)
精彩评论