Jump to content

BBCODE Regex


tinker

Recommended Posts

Hi, i'm just tinkering with a wysiwyg editor. I've just finished the JS side and thought the php would be easier, however it's not handling the way I expected it to!

Basically say there's multiple bold tags within a given text, then it takes just the first and very last, not each and every set.

 

 

function phpbbcode_convert($s)
{
$sret = "";
$pattern = array('/\\n/im',
				'/\[b\](.*)\[\/b\]/im',
				'/\[i\](.*)\[\/i\]/im',
				'/\[u\](.*)\[\/u\]/im',
				'/\[h1\](.*)\[\/h1\]/im'
				);
$replacement = array('<br>',
					'<b>$1</b>',
					'<i>$1</i>',
					'<u>$1</u>',
					'<h1>$1</h1>'
					);
$sret = preg_replace($pattern, $replacement, $s);
return $sret;
}

and let's say I call it with this...

$test = "[b]Hello[/b] [i]this[/i] [u]is a[/u] [b]test[/b]
[h1]mytitle[/h1]
[H1]mytitle[/H1]
[b]test[/b]
[b]test[/b]";
print "POST: <br>".phpbbcode_convert($test)."<br>";

 

When using JS I use the pattern modifiers 'ig'.

 

Any help appreciated!

 

P.S. I can accomplish these in a simpler manner, however the simpler way won't be any good for the more complex variations needed later...

Link to comment
https://forums.phpfreaks.com/topic/83279-bbcode-regex/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.