Home  >  Article  >  Backend Development  >  php 正则中运用函数的方式

php 正则中运用函数的方式

WBOY
WBOYOriginal
2016-06-13 13:15:43889browse

php 正则中使用函数的方式

//正则案例:使用php函数来改变字符值 
$string = "April 15, 2003"; 
 
//注意:使用模式修正符 /e 其作用是将匹配结果用作表达式,须使用它才可以使用\\3这样模式变量 
$pattern = "/(\w+) (\d+), (\d+)/e"; 
 
// 此处的模式变量\\2将被strtoupper转换为大写字符 : 注意函数当中用单引号连接 '字符串'.函数('模式变量').'字符串' 
$replacement = "'\\2'.strtoupper('\\1').'1,\\3'"; 
 
$s = preg_replace($pattern, $replacement, $string); 
echo $s;//输出 15APRIL1,2003 
?
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