|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-11-28 06:32 UTC] jf at jonathanfoote dot com
Description: ------------ --- From manual page: http://www.php.net/function.preg-replace --- Example #4 from preg_replace() it states: This would capitalize all HTML tags in the input text. But it does not mention that double quotes would get slashes added. Using the example would result in the text: <div class="test"> to be converted to: <DIV class=\"test\"> The tag is indeed converted to uppercase as it states, but slashes are added to double quotes. See below for perhaps a more robust example that accounts for this (basically run the rest through str_replace() that replaces \" with "). Test script: --------------- <?php $html_body = '<div class="test">'; //Solution from example #4 $html_upper = preg_replace("/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtoupper('\\2').'\\3'", $html_body); echo $html_upper; //resulted output: <DIV class=\"test\"> //Better solution: $html_upper = preg_replace("/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtoupper('\\2').str_replace('\\\"','\"','\\3')", $html_body); echo $html_upper; //resulted output: <DIV class="test"> ?> PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Fri Jan 02 09:00:01 2026 UTC |
I'm finding that it doesn't escape any of the quotes at all. That seems to be because the backreference that's being run through the strtoupper function is does not contain either of the tag's attributes. It would only escape any quotes in the tag name, it would seem. <?php $html_body = "<div class=\"test\" style='display: none;' >"; preg_replace("/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtoupper('\\2').'\\3'", $html_body); echo $html_body; // Outputs: <div class="test" style='display: none;'>