Coolcrab
Coolcrab

Reputation: 2723

Preg_replace issue bbcode

I have a problem with the preg_replace function, I am trying to convert some bbcode into HTML but it is not working. The one that I am trying to get to work is [size=200:37pfziz0][TEXT][/size:37pfziz0] (not sure why those numbers appear there but they need to be taken into consideration. So I am trying to do [size=(1):(2)](3)[/size:(4)]

I tried it with both eregi and preg and both don't seem to work:

$txt = eregi_replace("\\[size=([^\\[]+):([^\\[]+)\\]([^\\[]*)\\[/size:([^\\[]+)\\]", "<font size=\"\\1\%\">\\3</font>", $txt);
$txt = preg_replace("#\[size\=(.*?):(.*?)\](.*?)\[/size:(.*?)\]#is", "<font size=\"\\1\%\">\\3</font>", $txt);

Can anybody tell me what I am doing wrong? I spent about an hour on doing quotes, which eventually came out fine using this method:

$txt = preg_replace("#\[quote\=(.*?):(.*?)\](.*?)\[/quote:(.*?)\]#is", "<blockquote>Quote by: \\1<br/>\\3</blockquote>", $txt);

Upvotes: 0

Views: 276

Answers (1)

martinczerwi
martinczerwi

Reputation: 2847

Okay, considering your example bbcode made it a bit difficult, here's what I came up with, after a bit of testing:

preg_replace( '/\[size=([^\]\:]+)\:([^\]]+)\](.*?)\[\/size\:([^\]]+)\]/is', '<font size="$1%">$3</font>', $text);

Small note on this: As you've given an example with [text] in between the codes I used (.*?) to match the stuff in between. You might want to change this, if you don't need to support square brackets in between your tags.

As you can probably see, I've got my own habbits when using regex. First I'm always using / as a delimiter, because I think escaping chars is a bit easier with this. Second I'm using dollar signs for matches, to keep it simple, and don't have too many slashes.

Upvotes: 1

Related Questions