请教大神preg_replace如何替换成preg_replace_callback
程序员文章站
2022-05-11 19:14:19
...
private function escapeReserved($query) { $search = array ("/({)(\w+)(})/e", "/({L#)([0-9]+)(})/e", "/(\:)(uid|session|file|access|mode|comment|desc|size|start|end)/e", ) $replace = array ("'\"\\1'.strtoupper('\\2').'\\3\"'", "'\"\\1'.strtoupper('\\2').'\\3\"'", "'\\1'.'db_'.'\\2'.'\\3'",) return preg_replace($search, $replace, $query); }
如何将preg_replace()替换成preg_replace_callback()呢
回复讨论(解决方案)
$s = '{Test}{L#123456}:uid';echo escapeReserved($s);// "{TEST}""{L#123456}":db_uidecho '
';echo test($s);//"{TEST}""{L#123456}":db_uidfunction escapeReserved($query){ $search = array ("/({)(\w+)(})/e", "/({L#)([0-9]+)(})/e", "/(\:)(uid|session|file|access|mode|comment|desc|size|start|end)/e"); $replace = array ("'\"\\1'.strtoupper('\\2').'\\3\"'", "'\"\\1'.strtoupper('\\2').'\\3\"'", "'\\1'.'db_'.'\\2'.'\\3'"); return preg_replace($search, $replace, $query); }function test($query){ $search = array ("/({)(\w+)(})/", "/({L#)([0-9]+)(})/", "/(\:)(uid|session|file|access|mode|comment|desc|size|start|end)/"); return preg_replace_callback($search, function($m) { if(isset($m[3])){ return '"'.$m[1].strtoupper($m[2]).$m[3].'"'; }else{ return "$m[1]db_$m[2]"; } }, $query);}
推荐阅读
-
preg_replace如何替换成preg_replace_callback
-
nginx_proxy - 请教大神:PHP或Nginx如何在301重定向中伪造referer?
-
$str=preg_replace("/[attach]/is","111",$str); 这一句如何把111替换成可以执行的PHP代码
-
nginx_proxy - 请教大神:PHP或Nginx如何在301重定向中伪造referer?
-
$str=preg_replace("/[attach]/is","111",$str); 这一句如何把111替换成可以执行的PHP代码
-
preg_replace怎么替换成preg_replace_callback
-
请教大神preg_replace如何替换成preg_replace_callback
-
preg_replace如何替换成preg_replace_callback
-
preg_replace怎么替换成preg_replace_callback
-
这段代码preg_replace函数替换成preg_replace_callback函数该怎么写?