Home  >  Article  >  Backend Development  >  preg_replace如何替换成preg_replace_callback

preg_replace如何替换成preg_replace_callback

WBOY
WBOYOriginal
2016-06-23 13:51:061111browse

小弟以前从事Delphi开发,被社会所迫学习了PHP,今天当头一棒直接蒙了。
这个怎么改啊,程序报错需要将preg_replace替换成preg_replace_callback

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


回复讨论(解决方案)

这太难了!!!!

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

报错
Object of class Closure could not be converted to string

那你原来就报错

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

不过_getFieldTable要改一下了,因为接收的是一个数组了。

附源码,大神帮我看看

**     *    获取查询时的字段列表     *     *    @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;    }    /**     *    解析字段所属     *     *    @author    Garbin     *    @param     string $owner     *    @return    string     */    function _getFieldTable($owner)    {        if ($owner == 'this')        {            return $this->alias;        }        else        {            $m =& m($owner);            if ($m === false)            {                /* 若没有对象,则原样返回 */                return $owner;            }            return $m->alias;        }    }

15,16行我是用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;


组合的查询语句是这个样子的
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]


正确的应该是这个样子
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

错误的问题是: user_priv.privs和user_priv.user_id和s.store_id重点引用“.”都没有了。

噢,漏了个点

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

最后一个,谢谢xuzuning大神。最后面的preg_replace怎么替换

    /**     * 替换模块中图片路径     *     * @author liupeng     * @param  string  $source 内容     * @return string     **/    function smarty_prefilter_preCompile($source)    {        $file_type = strtolower(strrchr($this->_current_file, '.'));        $tmp_dir = '' ;        /* 替换文件编码头部 */        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(            '/<!--[^>|\n]*?({.+?})[^<|{|\n]*?-->/', // 替换smarty注释            '/<!--[^<|>|{|\n]*?-->/',               // 替换不换行的html注释            '/(href=["|\'])\.\.\/(.*?)(["|\'])/i',  // 替换相对链接            '/((?:background|src)\s*=\s*["|\'])(?:\.\/|\.\.\/)?(images\/.*?["|\'])/is', // 在images前加上 $tmp_dir            '/((?:background|background-image):\s*?url\()(?:\.\/|\.\.\/)?(images\/)/is', // 在images前加上 $tmp_dir            '/{nocache}(.+?){\/nocache}/ise', //无缓存模块            );        $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);	    }

需要修改的只是最后一对:

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

徐总给力啊,

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

/e 就是 eval("return 串")
你脱去外层的引号就对了

function($r) {
  return $this->select($r[1]);
}

Statement:
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