Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2004
    Posts
    194
    Thanks
    5
    Thanked 0 Times in 0 Posts

    RegEx - Make HTML Lower Case Except Attribute Values

    I'm in the middle of writing a function at the moment that uses a series of regular expressions to clean up some dynamically generated HTML from a WYSIWYG editor but I'm having a problem getting the HTML to convert to lower case correctly.

    I have the following expression to convert the tags to lower case:

    Code:
    $strHTML = preg_replace("/(<[^>]+>)/ies", "strtolower('$1')", $strHTML);
    Naturally of course this changes everything about the tag to lower case, the tag itself, the attributes and the attribute values which is all good except that I dont want it to convert the attribute value to lower case.

    For instance if I have the following tag:

    Code:
    <abbr title="What You See Is What You Get">WYSIWYG</abbr>
    I'd need the abbr opening and closing tag as well as the title attribute to be lower case, but the actual value of the title attribute "What You See Is What You Get" to remain as it is without being touched by the expression. Thats what's giving me a headache since it needs to work for any tag and attribute.

    Been Google'ing this for hours and I can't find anything that can help, anyone have any ideas?

    Thanks in advance.

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    194
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Actually nevermind, I've figured it out, I modified the regular expression line to use preg_replace_callback:

    Code:
    $strHTML = preg_replace_callback("/(<[^>]+>)/i", "lowerCaseHTML", $strHTML);
    And then wrote the following function:

    Code:
    function lowerCaseHTML($Matches) {
    
        if (preg_match("/<([^>]+)(\s\w+)=([^>]+)>/i", $Matches[1], $NewMatch)) {
            return "<" . strtolower($NewMatch[1]) . strtolower($NewMatch[2]) . "=" . $NewMatch[3] . ">";
    
        } else {
            return strtolower($Matches[1]);
    
        }
    
    }
    Seems to be working fine.
    Last edited by Acid; 02-25-2007 at 01:28 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •