请教大神preg_replace如何替换成preg_replace_callback

WBOY
Release: 2016-06-20 12:29:49
Original
890 people have browsed it

 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);  	  }
Copy after login


如何将preg_replace()替换成preg_replace_callback()呢


回复讨论(解决方案)

$s = '{Test}{L#123456}:uid';echo escapeReserved($s);// "{TEST}""{L#123456}":db_uidecho '<br>';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);}
Copy after login

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template