Regular Expression to lower case

Discussion in 'PHP' started by THT, Sep 14, 2005.

  1. #1
    Im trying to make strings matched by a regular expression, convert to lower case

    eg: <TABLE> to <table> and <B> to <b>

    why doesnt the regexp below work?

    $dic= preg_replace("/<([A-Z]*)>/",strtolower("$1"),$dic);

    Thanks
    THT
     
    THT, Sep 14, 2005 IP
  2. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to tell preg_replace that the replacement string is PHP code not a string, so use the "e" modifier, and enclose the strtolower bit in quotes.
    $dic= preg_replace("/<([A-Z]*)>/e","strtolower('$1')",$dic);
    Code (markup):
    cheers

    John
     
    johnt, Sep 14, 2005 IP
  3. THT

    THT Peon

    Messages:
    686
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    cheers john,

    just one last question:


    How do i handle this:
    <IMG
    ALT="" SRC="http://cache.lexico.com/dictionary/graphics/AHD4/GIF/abreve.gif" height="15" width="7" ALIGN="BOTTOM">
    
    Code (markup):
    cos of the newline?
     
    THT, Sep 14, 2005 IP
  4. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The newline is not a problem per se, its just treated like any other character, so the "ALT=..." etc will also cause problems. If you only want to lowercase the HTML tags, and not the attribute names also, you could use something like
    $dic=  preg_replace("/<\/?([A-Z]+)[^>]*>/Ue", "strtolower('$1')",$dic);
    Code (markup):
    The "\/?" should cope with the closing tags too, and the U flag tells it not to be greedy - i.e. only get the first closing >

    Haven't ahd chance to test it but I think it should be OK
     
    johnt, Sep 14, 2005 IP
    THT likes this.
  5. THT

    THT Peon

    Messages:
    686
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks john

    I know i said last question, but....

    Ive added a line abve as i need diferent behaviour for a tag:

    $dic= preg_replace("/<img(.*)>/ei","strtolower('<img'.\"$1\".' />')",$dic);
    Code (markup):
    should take

    <img blah blah blah>

    and make it

    <img blah blah blah />

    but it doesnt..... help?
     
    THT, Sep 14, 2005 IP
  6. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Two possibilities that I can think of. Either the escaped and unescaped quotes are confusing it ( they confused me ;) ), or it is the greediness again - matching the last > that it finds instead of the first.
    try
    $dic= preg_replace("/<img(.*)>/eiU","strtolower('<img$1' />')",$dic);
    Code (markup):
     
    johnt, Sep 14, 2005 IP
  7. THT

    THT Peon

    Messages:
    686
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    John, you are wonderful!

    one last one (honest this time!)

    I wan to find image tags that dont have alt tags and add a blank alt tags (alt="")

    Whats the best way to do this?
     
    THT, Sep 14, 2005 IP
  8. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Don't know how to match negative patterns, but you could hack it by writing a function to check using strpos. Something along the lines of
    function checkForAltTag ( $imageString ) {
    if ( strpos( $imageString, "alt=" ) === false ) {
        return str_replace("<img", "<img alt=\"\"", $imageString);
    } else {
        return $imageString;
    }
    } // end checkForAltTag
    
    $dic= preg_replace("/(<img.*>)/eiU","checkForAltTag($1)",$dic);
    
    Code (markup):
    I'm sure that there's a more elegant way of doing this, but I can't think of it right now

    hope it helps

    John
     
    johnt, Sep 14, 2005 IP