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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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