dongtaotao19830418 2015-02-18 13:40
浏览 49

将/ e修饰符替换为preg_replace_callback的问题

I am having some trouble with replacing preg_replace with /e modifier to preg_replace_callback in this function:

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

Can you help me with this?

EDIT:

I managed to fix the second one, but the other

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

did not work. I got following error message:

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')

I am still relatively new to php so i am not sure how to handle this issue.

  • 写回答

3条回答 默认 最新

  • dp198879 2015-02-19 08:39
    关注

    Finally I understood what you are trying to do. The callback function should receive an array of all the matches or a single match (depending on what was found). So it would look something like this: $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 ); If you need some outside variable inside the anonymous function then declare the callback function as function($match) use ($varname)

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么