Regular expressions

This is a discussion on Regular expressions within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I'm stumped working with regular expressions in php...I'm trying to convert the following: [color=blue] to: <font color="blue"> and I've tried ...

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903

    Regular expressions

    I'm stumped working with regular expressions in php...I'm trying to convert the following:

    [color=blue]

    to:

    <font color="blue">


    and I've tried doing this in php:

    Code:
    $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.
    Last edited by jverkoey; 01-23-2005 at 04:55 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    ([[:alpha:]])+

    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Quote Originally Posted by Salem
    I've no idea what the + in =+ is doing.
    [color=blue]


    -edit-
    Ahah, that did work though, thanks salem

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Another question....Now I'm trying to use a different tag and it's acting really...err....weird....

    If I use this code:
    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:
    Code:
    $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......

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    OK....now this is just plain weird, every time I try and even do:

    Code:
      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
    Last edited by jverkoey; 01-23-2005 at 06:46 PM.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    This interest you at all?

    http://www.weitz.de/regex-coach/

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by jverkoey
    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:
    Code:
    <?
    	print("<span style=\"test\">");
    ?>
    and everything worked fine... I use firefox too. Try it with just one php file if you can.

  8. #8
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    nonono

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

    Code:
    <?php
    
         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.
    Last edited by jverkoey; 01-23-2005 at 09:01 PM.

  9. #9
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    It added the </span> ending tag
    Code:
    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!!

  10. #10
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regular expressions [Boost]
    By Desolation in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2006, 10:10 PM
  2. Regular expressions
    By JimpsEd in forum C Programming
    Replies: 5
    Last Post: 05-13-2006, 07:01 PM
  3. Help please: regular expressions in C++
    By reivaj7999 in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2005, 02:11 AM
  4. Regular Expressions
    By Korn1699 in forum C# Programming
    Replies: 4
    Last Post: 01-12-2005, 12:50 AM
  5. regular expressions help
    By axon in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-09-2004, 08:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21