Simple Machines Community Forum

General Community => Scripting Help => Topic started by: Curbow 5 on September 29, 2003, 09:09:24 AM

Title: BB-code regex help needed!
Post by: Curbow 5 on September 29, 2003, 09:09:24 AM
I use the following coding for [ size ] and [ color ] bb-code:


$message = eregi_replace("\\[size=([^\\[]*)\\]([^\\[]*)\\[/size\\]","<span style=\"font-size: \\1px;\">\\2</span>",$message);   
$message = eregi_replace("\\[color=([^\\[]*)\\]([^\\[]*)\\[/color\\]","<font color=\"\\1\">\\2</font>",$message);


When I input the following bb-codes, only the first one really works, the second one only works for the [ color ] and the other two don't do anything:


[color=red][size=18]test[/size][/color]
[size=18][color=red]test[/color][/size]
[size=18][color=red]test[/size][/color]
[color=red][size=18]test[/color][/size]


I don't really understand what the problem is... If anyone is willing to take a look at the coding and tell what I did wrong, I'd be really gratefull!  :D
Title: Re:BB-code regex help needed!
Post by: Shadow on September 29, 2003, 10:45:59 AM
Just wondering, why don't you just use the <font> tag for the size? :-/
Title: Re:BB-code regex help needed!
Post by: Curbow 5 on September 29, 2003, 10:51:53 AM
i'm not quite sure.... but would it make a difference?
Title: Re:BB-code regex help needed!
Post by: Chris Cromer on September 29, 2003, 11:29:06 AM
Less html... but other than that no.
Title: Re:BB-code regex help needed!
Post by: Spaceman-Spiff on September 29, 2003, 12:03:10 PM
ereg and eregi are much slower than preg
if you can, you should use preg_replace instead
Title: Re:BB-code regex help needed!
Post by: Haase on September 29, 2003, 12:55:03 PM
I'm pretty sure that the last two *shouldn't* work.  It may be possible, but I think it's bad practice to start tag A and then start tag B and then close tag A without closing tab B first.  Tags should be nested or not nested.  It seems like this happens most often with < form > tags and < tables >.  Although there's probably nothing wrong with that, it really bugs me when I see that.

I have a feeling that one function is taking precedence over the other.  That is, in your code you are first doing size, then color.  your first BBC example works.  I bet if you switched your logic so that you were doing color first, your second BBC would be the one working correctly.

Probably what's happening is that when you first do size, its doing something to mess up the tag that's nested inside it.
Title: Re:BB-code regex help needed!
Post by: [Unknown] on September 29, 2003, 03:28:06 PM
$message = preg_replace('~\[size=(.+?)\](.+?)\[/size\]~is','<span style="font-size: $1px;">$2</span>', $message);  
$message = preg_replace('~\[color=(.+?)\](.+?)\[/color\]~is','<font color="$1">$2</font>', $message);


-[Unknown]