【问题标题】:Problems with replacing /e modifier to preg_replace_callback将 /e 修饰符替换为 preg_replace_callback 的问题
【发布时间】:2015-04-19 12:56:44
【问题描述】:

在此函数中将 preg_replace 替换为 /e 修饰符到 preg_replace_callback 时遇到了一些问题:

    private function parseFunctions() {
    // replaces includes ( {include file="..."} )
    while( preg_match( "/" .$this->leftDelimiterF ."include file=\"(.*)\.(.*)\""
                       .$this->rightDelimiterF ."/isUe", $this->template) )
    {
        $this->template = preg_replace( "/" .$this->leftDelimiterF ."include file=\"(.*)\.(.*)\""
                                        .$this->rightDelimiterF."/isUe",
                                        "file_get_contents(\$this->templateDir.'\\1'.'.'.'\\2')",
                                        $this->template );
    }


    // deletes comments from the template files
    $this->template = preg_replace( "/" .$this->leftDelimiterC ."(.*)" .$this->rightDelimiterC ."/isUe",
                                    "", $this->template );
    }

你能帮我解决这个问题吗?

编辑:

我设法修复了第二个,但另一个

{
        $this->template = preg_replace_callback( "/" .$this->leftDelimiterF ."include file=\"(.*)\.(.*)\""
                                        .$this->rightDelimiterF."/isU",
                                        function(){$replacement="file_get_contents(\$this->templateDir.'\\1'.'.'.'\\2')";
                                        return $replacement;},
                                        $this->template );
    }

没用。 我收到以下错误消息:

file_get_contents($this->templateDir.'\1'.'.'.'\2') file_get_contents($this->templateDir.'\1'.'.'.'\2') file_get_contents($this->templateDir.'\1'.'.'.'\2') file_get_contents($this->templateDir.'\1'.'.'.'\2') file_get_contents($this->templateDir.'\1'.'.'.'\2') file_get_contents($this-> templateDir.'\1'.'.'.'\2')

我对php还是比较陌生,所以我不知道如何处理这个问题。

【问题讨论】:

  • 我不明白你在做什么,你为什么要在 while 中替换,然后在循环本身中替换。只需其中一个就足够了。
  • 我也不确定。原来不是我写的,我只需要处理一下
  • 那么您至少可以更好地解释问题所在吗?
  • 问题是,/e 修饰符已被弃用(preg_replace():/e 修饰符已弃用,请改用 preg_replace_callback)。我正在尝试纠正代码中的一些问题,这些问题都留下了......

标签: php preg-replace preg-replace-callback


【解决方案1】:

我终于明白你要做什么了。 回调函数应该接收所有匹配的数组或单个匹配(取决于找到的内容)。 所以它看起来像这样: $this->template = preg_replace_callback( "/" .$this->leftDelimiterF ."include file=\"(.*)\.(.*)\"".$this->rightDelimiterF."/isU", function($match){ $replacement="file_get_contents(\$this->match.'\\1'.'.'.'\\2')"; return $replacement;}, this->template ); 如果您需要匿名函数内部的一些外部变量,则将回调函数声明为 function($match) use ($varname)

【讨论】:

  • 工作正常,直到站点尝试加载模板文件。然后我收到错误消息: file_get_contents($this->match.'\1'.'.'.'\2') file_get_contents($this->match.'\1'.'.'.'\2' ) file_get_contents($this->match.'\1'.'.'.'\2') file_get_contents($this->match.'\1'.'.'.'\2') file_get_contents($this- >match.'\1'.'.'.'\2') file_get_contents($this->match.'\1'.'.'.'\2')
  • “加载模板”是什么意思?
  • 所有 html 内容都包含在 .tpl 文件中。 {include file="assideleft.tpl"} {include file="middlecontent.tpl"} {include file="assideright.tpl"} 函数将其替换为 .tpl 文件的内容
  • 抱歉,我现在知道了。而不是 function($match) 做 'function($matches)` 和里面而不是 \\1\\2 尝试 $match[0]match[1] 相应
【解决方案2】:

您有 2 个不同的变量 $match$matches。这是一个工作代码:

$this->template = preg_replace_callback(
    "/".$this->leftDelimiterF.'include file="([^"]+)"'.$this->rightDelimiterF."/isU",
     function($match){
         $replacement = "file_get_contents(\$this->templateDir.$match[1])";
         return $replacement;
     },
$this->template );

【讨论】:

  • 我试过了,至少我得到了一个全新的错误消息:Catchable fatal error: Object of class Closure could not be convert to string in C:\xampp\htdocs\mxframework\mxsframework\data \system\classes\view\cl_template.php 在第 288 行
  • 我加了一点改动: { $this->template = preg_replace_callback( "/".$this->leftDelimiterF.'include file="([^"]+)"'.$this ->rightDelimiterF."/isU", function($match){ $replacement = "file_get_contents(\'$this->templateDir'.$match[1])"; return $replacement; }, $this->template ) ; } 错误信息现在是:file_get_contents(\'templates/officelook2013/tplfiles/'.header.tpl)
【解决方案3】:
$this->template = preg_replace_callback("/" .$this->leftDelimiterF ."include file=\"(.*)\.(.*)\""
     .$this->rightDelimiterF."/isU", 
     function ($m) { return file_get_contents($this->templateDir.$m[1].'.'.$m[2]);}, 
     $this->template);

【讨论】:

  • 您能否简要说明您的代码?谢谢!
猜你喜欢
  • 2014-02-16
  • 2013-03-05
  • 2015-07-31
相关资源
最近更新 更多