How to replace preg_replace with preg_replace_callback

Source: Internet
Author: User
How to replace preg_replace with preg_replace_callback
 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);    }


How can I replace preg_replace () with preg_replace_callback ()?


Reply to discussion (solution)

$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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.