时间:2021-07-01 10:21:17 帮助过:20人阅读
$string = "april 15, 2003";$pattern = "/(/w+) (/d+), (/d+)/i";$replacement = "/${1}1,/$3";print preg_replace($pattern, $replacement, $string);/* output ======april1,2003*/
$patterns = array ("/(19|20)(/d{2})-(/d{1,2})-(/d{1,2})/", "/^/s*{(/w+)}/s*=/");$replace = array ("//3///4///1//2", "$//1 =");print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");
${1} 就是匹配第一个 , $3就是匹配第三个。
${1} 就是匹配第一个 , $3就是匹配第三个。
${1} 就是匹配第一个 , $3就是匹配第三个。
第一段代码是错误的,应写作
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = "\${1}1,\$3";print preg_replace($pattern, $replacement, $string);或
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = '${1}1,$3';print preg_replace($pattern, $replacement, $string);
$patterns = array ("/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/", "/^\s*{(\w+)}\s*=/");$replace = array ("\\3/\\4/\\1/\\2", "$\\1 =");print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");
第一段代码是错误的,应写作
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = "\${1}1,\$3";print preg_replace($pattern, $replacement, $string);或
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = '${1}1,$3';print preg_replace($pattern, $replacement, $string);
$patterns = array ("/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/", "/^\s*{(\w+)}\s*=/");$replace = array ("\\3/\\4/\\1/\\2", "$\\1 =");print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");
双引号中 php 会试图将 $ 解释为变量的前导
字符串 "${1}" 将引起变量未定义的错误
所以要转义掉,这样才会把 ${1} 交给 prea_replace 去处理
双引号中 php 会试图将 $ 解释为变量的前导
字符串 "${1}" 将引起变量未定义的错误
所以要转义掉,这样才会把 ${1} 交给 prea_replace 去处理