php – 如何在preg_replace中用c(xxx)替换c(xxx)?
发布时间:2020-12-13 17:24:59 所属栏目:PHP教程 来源:网络整理
导读:如何在preg_replace中用c(xxx)替换c(xxx)? 在下面的代码中,我想用functioun c(xxx)替换str c(xxx). 我没有得到我想要的正确结果. 我的代码出了什么问题?以及如何解决? ???? $c['GOOD']='very good';$c['BOY']='jimmy';function c($x){ global $c; if(isse
如何在preg_replace中用c(xxx)替换c(xxx)?
在下面的代码中,我想用functioun c(xxx)替换str c(xxx). $c['GOOD']='very good'; $c['BOY']='jimmy'; function c($x){ global $c; if(isset($c[$x])){ return $c[$x]; } } $str="hello c(GOOD) world c(BOY) "; $str=preg_replace("@c(([A-Z_d]+))@",c('$1'),$str); echo $str; // --> hello world // how to get hello very good world jimmy 解决方法
试试e修饰符
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
<?php $str = preg_replace( "/c(([A-Z_d]+))/e",'c("$1")',$str ); ?> 更好地使用preg_replace_callback <?php $str = preg_replace_callback( "/c(([A-Z_d]+))/",function( $m ) { return c( $m[1] ); },$str ); ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |