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
https://forums.phpfreaks.com/topic/234572-preg-replace-and-bb-codes/
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.