How to replace preg_replace with preg_replace_callback

Source: Internet
Author: User
Tags html comment
How to replace preg_replace with preg_replace_callback: my younger brother was engaged in Delphi development and was forced to learn PHP by the society. today, I am overwhelmed.
How can this problem be solved? if the program reports an error, replace preg_replace with preg_replace_callback.
$fields = preg_replace('/([a-zA-Z0-9_]+)\.([a-zA-Z0-9_*]+)/e', "\$this->_getFieldTable('\\1') . '.\\2'", $fields);


Reply to discussion (solution)

This is too difficult !!!!

$fields = preg_replace('/([a-zA-Z0-9_]+)\.([a-zA-Z0-9_*]+)/', function($r) {  return $this->_getFieldTable($r[1]) . $r[2];  }, $fields);

$fields = preg_replace('/([a-zA-Z0-9_]+)\.([a-zA-Z0-9_*]+)/', function($r) {  return $this->_getFieldTable($r[1]) . $r[2];  }, $fields);

Error
Object of class Closure cocould not be converted to string

Then you would have reported an error.

$ Fields = preg_replace_callback ('/([a-zA-Z0-9 _] + )\. ([a-zA-Z0-9 _ *] +)/', array ($ this, "_ getFieldTable"), $ fields );

However, you have to modify _ getFieldTable because it receives an array.

With source code, please help me see

* ** Obtain the field list during query ** @ author Garbin * @ param string $ src_fields_list * @ return string */function getRealFields ($ src_fields_list) {$ fields = $ src_fields_list; if (! $ Src_fields_list) {$ fields = '';} // $ fields = preg_replace ('/([a-zA-Z0-9 _] + )\. ([a-zA-Z0-9 _ *] +)/E', "\ $ this-> _ getFieldTable ('\ 1 '). '. \ 2 '", $ fields); $ fields = preg_replace_callback ('/([a-zA-Z0-9 _] + )\. ([a-zA-Z0-9 _ *] +)/', function ($ r) {return $ this-> _ getFieldTable ($ r [1]). $ r [2] ;}, $ fields); // $ fields = preg_replace_callback ('/([a-zA-Z0-9 _] + )\. ([a-zA-Z0-9 _ *] +)/', function ($ r) {return $ this-> _ getFieldTable ($ r (1 ). $ r (2) ;}, $ fields); return $ fields ;} /*** resolve the field ** @ author Garbin * @ param string $ owner * @ return string */function _ getFieldTable ($ owner) {if ($ owner = 'eas') {return $ this-> alias;} else {$ m = & m ($ owner ); if ($ m = false) {/* if no object exists, return the original */return $ owner;} return $ m-> alias ;}}

Lines 15 and 16 I use xuzuning.


        //$fields = preg_replace('/([a-zA-Z0-9_]+)\.([a-zA-Z0-9_*]+)/e', "\$this->_getFieldTable('\\1') . '.\\2'", $fields);        $fields = preg_replace_callback('/([a-zA-Z0-9_]+)\.([a-zA-Z0-9_*]+)/', function($r) {return $this->_getFieldTable($r[1]) . $r[2];}, $fields);           //$fields = preg_replace_callback('/([a-zA-Z0-9_]+)\.([a-zA-Z0-9_*]+)/',function($r){ return $this->_getFieldTable($r(1) . $r(2));}, $fields);        return $fields;


The combined query statement looks like this.
MySQL Error[1054]: Unknown column 'user_privprivs' in 'field list' MySQL Query:SELECT user_privprivs, sstore_name,user_privuser_id,s.store_id FROM ecm_store s LEFT JOIN ecm_user_priv user_priv ON s.store_id = user_priv.store_id WHERE user_priv.user_id IN ('1') ORDER BY sstore_id DESC Wrong File: \eccore\model\mysql.php[534]


This should be the correct one.
SELECT user_priv.privs, s.store_name,user_priv.user_id,s.store_id FROM ecm_store s LEFT JOIN ecm_user_priv user_priv ON s.store_id = user_priv.store_id WHERE user_priv.user_id IN ('1') ORDER BY s.store_id DESC

The error is: user_priv.privs, user_priv.user_id, and s. store_id cannot be referenced.

Oh, missed a point.

$fields = preg_replace('/([a-zA-Z0-9_]+)\.([a-zA-Z0-9_*]+)/', function($r) {  return $this->_getFieldTable($r[1]) . ".$r[2]";  }, $fields);

Last one, thank you, xuzuning. How to replace preg_replace

/*** Replace the image path in the module ** @ author liupeng * @ param string $ source content * @ return string **/function smarty_prefilter_preCompile ($ source) {$ file_type = strtolower (strrchr ($ this-> _ current_file ,'. '); $ tmp_dir = '';/* replace the file encoding header */if (strpos ($ source," \ xEF \ xBB \ xBF ")! = FALSE) {$ source = str_replace ("\ xEF \ xBB \ xBF", '', $ source);} if ($ this-> store_id> 0) {if (strpos ($ this-> _ current_file, '/mall/resource ')! = False) {$ mall_skin = $ this-> options ['Mall _ skin']; $ tmp_dir = "themes/mall/skin/$ mall_skin /";} else {$ tmp_dir = "themes/store/skin /". $ this-> skin. '/';} else {$ tmp_dir = "themes/mall/skin /". $ this-> skin. '/';} $ pattern = array ('/
 /', // Replace the smarty comment '/
 /', // Replace the html comment without line breaks'/(href = ["| \ ']) \. \. \/(. *?) (["| \ '])/I', // replace the relative link '/((? : Background | src) \ s * = \ s * ["| \ ']) (? : \. \/| \.\.\/)? (Images \/.*? ["| \ '])/Is', // add $ tmp_dir '/((? : Background | background-image): \ s *? Url \()(? : \. \/| \.\.\/)? (Images \/)/is ', // add $ tmp_dir'/{nocache} (. +?) to images ?) {\/Nocache}/ise ', // no Cache module); $ replace = array (' \ 1', '', '\ 1 \ 2 \ 3 ', '\ 1 '. $ tmp_dir. '\ 2',' \ 1 '. $ tmp_dir. '\ 2', "' {insert name = \" nocache \"'. '". $ this-> _ echash. "'. base64_encode ('\ 1 '). '}' ",); return preg_replace ($ pattern, $ replace, $ source );}

You only need to modify the last pair:

$source = preg_replace_callback( '/{nocache}(.+?){\/nocache}/is', function($r) {    return '{insert name="nocache" '  . $this->_echash .  base64_encode($r[1]) . '}'; }, $source);

Eric Xu is awesome,

    function fetch_str($source)    {        if (!defined('IS_BACKEND'))        {            $source = $this->smarty_prefilter_preCompile($source);        }        return preg_replace("/{([^\}\{\n]*)}/e", "\$this->select('\\1');", $source);    }

/E is eval ("return string ")
You can just remove the outer quotation marks.

Function ($ r ){
Return $ this-> select ($ r [1]);
}

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.