Doc #60401 [Csd]: Example 4 results in double quotes slashed

From: Date: Sat, 03 Dec 2011 07:31:35 +0000
Subject: Doc #60401 [Csd]: Example 4 results in double quotes slashed
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-7514@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=60401&edit=1 ID: 60401 User updated by: jf at jonathanfoote dot com Reported by: jf at jonathanfoote dot com Summary: Example 4 results in double quotes slashed Status: Closed Type: Documentation Problem Package: Documentation problem Operating System: Windows PHP Version: 5.3.8 Assigned To: frozenfire Block user comment: N Private report: N New Comment: Since it surrounds \\3 with single quotes, the end resulting string does not have single quotes escaped, just double quotes and I assume null. If for example, you were to run example #4 on the following: <div class="test" style='display: none;'> result: <DIV class=\"test\" style='display: none;'> Previous Comments: ------------------------------------------------------------------------ [2011-12-03 04:28:36] frozenfire@php.net This bug has been fixed in the documentation's XML sources. Since the online and downloadable versions of the documentation need some time to get updated, we would like to ask you to be a bit patient. Thank you for the report, and for helping us make our documentation better. ------------------------------------------------------------------------ [2011-12-03 04:28:24] frozenfire@php.net Automatic comment from SVN on behalf of frozenfire Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=320298 Log: Clarified e-modifier example behavior, as per bug #60401. ------------------------------------------------------------------------ [2011-11-28 06:32:45] 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"> ?> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=60401&edit=1

« previous php.doc.bugs (#7514) next »