View Full Version : Regular expressions

01-23-2005, 04:52 PM
I'm stumped working with regular expressions in php...I'm trying to convert the following:



<font color="blue">

and I've tried doing this in php:

$text = eregi_replace("\[color=+([[:alpha:]])+\]","&lt;font color=\"\\1\">",$text);

which gave me:

<font color="e">

so....I'm stumped, as I don't know how to grab the string "red" from [color=red]....anyone here good with regular expressions? I've just started working with 'em today and I'm still trying to figure 'em out.

01-23-2005, 04:57 PM

Move the + inside the (), if you want to match an alpha string, rather than just one letter.

I've no idea what the + in =+ is doing.

01-23-2005, 04:59 PM
I've no idea what the + in =+ is doing.

Ahah, that did work though, thanks salem

01-23-2005, 06:06 PM
Another question....Now I'm trying to use a different tag and it's acting really...err....weird....

If I use this code:

$text = eregi_replace("\[color([[:space:]]+)?=([[:space:]]+)?([[:alnum:]#]+).\]","span style=\"color: \\3;\">",$text);

It will turn this:

[color = #FF0000]

In to

span style="color: #FF000;">

However, if I add the < to the expression so it becomes:

$text = eregi_replace("\[color([[:space:]]+)?=([[:space:]]+)?([[:alnum:]#]+).\]","<span style=\"color: \\3;\">",$text);

Notice the <span in the second string now

It no longer turns the code in to what it should be and just outputs:

<span style="">

Which seems *really* weird.....and I can't figure out why it would mess with my output string like that......

01-23-2005, 06:31 PM
OK....now this is just plain weird, every time I try and even do:

print("<span style=\"test\">");

It reformats the resulting html to be:

<span style="">

what the heck is going on???

Ok...there seems to be a bug with firefox where if you select code and right click and "view selection source" it screws with your source code....hmm...weird

01-23-2005, 07:59 PM
This interest you at all?


01-23-2005, 08:41 PM
It reformats the resulting html to be:

<span style="">

what the heck is going on??? That is a really good question, because I made a single php file and put:

print("<span style=\"test\">");
?> and everything worked fine... I use firefox too. Try it with just one php file if you can.

01-23-2005, 08:57 PM

Do that same code, but but some text before and after it like so:


echo 'blah<span style="color: red;">orblah';


Then select blahorblah and right click "View selection source" and there ya go, it cut out the tag!

Ahhah! I found the problem, in my code when I got the information from the database I forgot to call StripTags which seems to cause problems with the ereg function. Now that I've called StripTags none of the problems are happening anymore.

01-23-2005, 09:03 PM
It added the </span> ending tag :(

blah<span style="color: red;">orblah</span> But when I first went page source everything was original, firefox is scary :(
I saw your edit! I'm glad things are working out. I remember when I had this one problem for a really long time, and in the end it happened to be a configuration in php.ini! ggrrraaa!!

01-23-2005, 09:36 PM
Oh, so you did see what I was talking about then....

I spent about an hour trying to figure it out, I thought PHP was modifying my code for some reason and it was really starting to freak me out and the whole time I'd been doing "view selection source".

I only happened to figure it out when i was playing with my new firefox gestures extension and up popped the full source and I saw that my code was exactly as it should have been. :confused: :confused: