note 47735 deleted from function.preg-replace by tularis
| From: | tularis@php.net | Date: | Sun, 18 Jun 2006 13:22:23 +0000 |
| Subject: | note 47735 deleted from function.preg-replace by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114305@lists.php.net to get a copy of this message | ||
Note Submitter: Yunbin Bai
----
If you want to use /e in the Reg expression, I think using escaped double quote is better than just
single quote in the replacement parameter.
I meet this problem when I try to replace a string that contains double quote in it, it cost me some
time to solve this. So I simplified my code and present here:
consider capitalize SSI directives in the input text:
$html_body = "<!--#include virtual=\"header.htm\" -->";
//That is, source text: <!--#include virtual="header.htm" -->
$html_body = preg_replace('/(<!--#)(.*)(-->)/e',
"'\\1'.strtoupper('\\2').'\\3'", $html_body);
echo $html_body;
//output text: <!--#INCLUDE VIRTUAL=\"HEADER.HTM\" -->
//Result is not what you want: there is additional backslash generated that is useless.
My Solution to this:
use \" instead of ', that make it a little mess, but garanteed working.
$html_body = preg_replace('/(<!--#)(.*)(-->)/e',
"\"\\1\".strtoupper(\"\\2\").\"\\3\"", $html_body);
echo $html_body;
//result is correct now: <!--#INCLUDE VIRTUAL="HEADER.HTM" -->