News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

BB-code regex help needed!

Started by Curbow 5, September 29, 2003, 09:09:24 AM

Previous topic - Next topic

Curbow 5

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

Shadow

Just wondering, why don't you just use the <font> tag for the size? :-/
Just do it, go Charter! [Unknown] offered me a spot on the dev team! I swear it!

"Sup foos'! I'm Marshie! Capital M and then arshie! I'm going this way!!!"

Curbow 5

i'm not quite sure.... but would it make a difference?

Chris Cromer

Less html... but other than that no.
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

Spaceman-Spiff

ereg and eregi are much slower than preg
if you can, you should use preg_replace instead

Haase

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.
Find out about Enigma, the portal built exclusively for YaBB SE will be continuing it's work towards SMF

[Unknown]

$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]

Advertisement: