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
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
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?
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
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?
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):
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?
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