Hi,

I've got a small problem here in replacing html for bb. I can go bb to html really easily (see the code below), but when I go the other way, I get the same text returned as when I put it in

if ($tobb == 0)
	{
        $search = array('/\[bold\](.*?)\[\/bold\]/is','/\[italic\](.*?)\[\/italic\]/is',
						'/\[underline\](.*?)\[\/underline\]/is',
                		'/\[link\](.*?)\[\/link\]/is',
               			'/\[link\=(.*?)\](.*?)\[\/link\]/is');

        $replace = array('<strong>$1</strong>','<em>$1</em>','<u>$1</u>',
                		 '<a href="$1">$1</a>','<a href="$1">$2</a>');
	}
	else
	{
        $replace = array('\[bold\](.*?)\[\bold\]','\[italic\](.*?)\[\italic\]',
						'\[underline\](.*?)\[\underline\]',
                		'\[link\](.*?)\[\link\]',
               			'\[link\=(.*?)\](.*?)\[\link\]');

        $search = array('/\<strong\>$1\<\/strong\>/is','/\<em\>$1\<\/em\>/is','/\<u\>$1\<\/u\>/is',
                		'/\<a href="$1"\>$1\<\/a\>/is','/\<a href="$1"\>$2\<\/a\>/is');
	}	
	
	$newtext = preg_replace($search, $replace, $text);

I'm using the following to test the code (in another file)

<?php 
$m = "[bold]Hello there[/bold] [italic]my wibblers hurt[/italic] [underline]I love becki[/underline] [link=www.bbc.co.uk/drwho]http://www.bbc.co.uk/drwho[/link]";
$n = convert($m, 0);
echo $n;
$b = convert($n, 1);
echo $b;
?>

I have a feeling that my regex is broken, but can't see where. Help!

Why don't you take a look at phpbb2 and phpbb3 how they handle bbcodes ?

You can also do a search at phpclasses

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.