soupagain
soupagain

Reputation: 1133

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 variable...

Upvotes: 0

Views: 1584

Answers (2)

user1441149
user1441149

Reputation:

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. ;-)

Upvotes: 0

John Sobolewski
John Sobolewski

Reputation: 4572

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...

Upvotes: 2

Related Questions