Jump to content

Preg Replace and BB Codes


ShoeLace1291

Recommended Posts

I've never really made my own bbcode parser function before, but I'm going off of different tutorials to help me create my own style of parsing.  I've actually never really used preg_replace much, either.  My code is returning an odd error and I have no idea how to fix it.  I'm using codeigniter and the error it returns is below:

 

preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 6

 

This is the code for my function:

	function bbParse($string){

	$codes = array(
			'/\[b\](*?)\[\/b\]/' => '<b>\\1</b>', //bold tags
			'/\[h2\](*?)\[/h2\]/' => '<h2>\\1</h2>', //h2 tags
			'/\[h3\](*?)[\/h3\]/' => '<h3>\\1</h3>', //h3 tags
			'/\[quote\](*?)\[\/quote\]/' => '<blockquote>\\1</blockquote>', //quote tags using the template's blockquote tag
			'/\[img\](*?)\[/img\]/' => '<img src=\'\\1\' alt=\'Image Not Available\'>', //image tags
			'/\[url\=(*?)\](*?)\[/url\]/' => '<a href=\'\\1\' title=\'\\2\'>\\2</a>', //anchor tags
			'/\[p\](*?)\[/p\]/' => '<p>\\1</p>' //paragraph tags
			);



		$string = preg_replace(array_keys($codes), array_values($codes), $string);

		return $string;


}

Link to comment
Share on other sites

Try:

 

'/\[b\](.+?)\[\/b\]/' => '<b>$1</b>', //bold tags

 

.+? = every character (.), one or more (+), ungreedy (?). The $1 marks the matched characters replace part. Try the same for each key=>value part of your regex and it should work fine.

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.