Home  >  Article  >  Backend Development  >  PHP的preg_replace函数的有关问题

PHP的preg_replace函数的有关问题

WBOY
WBOYOriginal
2016-06-13 12:04:20902browse

PHP的preg_replace函数的问题

$string = "april 15, 2003";<br />$pattern = "/(/w+) (/d+), (/d+)/i";<br />$replacement = "/${1}1,/$3";<br />print preg_replace($pattern, $replacement, $string);<br />/* output<br />   ======<br />april1,2003<br />*/


$replacement = "/${1}1,/$3";
"/${1}1,/$3"这个是什么意思?


<br />$patterns = array ("/(19|20)(/d{2})-(/d{1,2})-(/d{1,2})/",<br />                   "/^/s*{(/w+)}/s*=/");<br />$replace = array ("//3///4///1//2", "$//1 =");<br />print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");<br />


$replace = array ("//3///4///1//2", "$//1 =");这个也是什么意思?
------解决方案--------------------
双引号中 php 会试图将 $ 解释为变量的前导
字符串 "${1}" 将引起变量未定义的错误
所以要转义掉,这样才会把 ${1} 交给 prea_replace 去处理

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn