从preg_replace到preg_replace_callback [英] FROM preg_replace TO preg_replace_callback

查看:124
本文介绍了从preg_replace到preg_replace_callback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我在将某些旧功能更新为preg_replace_callback时遇到问题。
编辑:我在做什么错了?

I'm having issues with updating some old function to preg_replace_callback. what am I doing wrong?

这是我的第一个功能(preg_replace / deprecated):

This is my first (preg_replace/deprecated) function:

if ($handle) {
while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    @eval('$templ = new '.TEMPL_CLASS.';');
    $buffer = preg_replace("#§([a-z0-9-_]+)\.?([a-z0-9-_]+)?#ie","\$templ->\\1(\\2)",$buffer);
    $out .= $buffer;
    }
fclose($handle);
}

第二个函数(这是我尝试转换为preg_replace_callback的尝试):

Second function (this is my attempt at converting to preg_replace_callback):

if ($handle) {
  while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    @eval('$templ = new '.TEMPL_CLASS.';');  
    $buffer = preg_replace_callback(
      '#§([\w-]+)\.?([\w-]+)?#',
      function ($m) {
        @$templ->$m[1]($m[2]);   
      },
      $buffer
    );
    $out .= $buffer;
  }
  fclose($handle);
}

OLD! M42的answear修复了以下错误:

OLD! M42's answear fixed the follow error:


警告:preg_replace_callback():修饰符/ e不能与/ b中的
替换回调一起使用var / www / inc / engine.php,第52行

Warning: preg_replace_callback(): Modifier /e cannot be used with replacement callback in /var/www/inc/engine.php on line 52



); <-- line 52
$out .= $buffer;

编辑:我不知道该如何处理渲染部分。

I dont know how to handle the render part of this..

$buffer = preg_replace("#§([a-z0-9-_]+)\.?([a-z0-9-_]+)?#ie","\$templ->\\1(\\2)",$buffer);

现在它正在呈现空白页面。我想错误是在

Right now it is rendering a blank page.. I guess the error is in


返回模板($ m [1],$ m [2]);

return templ($m[1], $m[2]);


推荐答案

如消息中所述,删除 e 修饰符:

As it's said in the message, removed the e modifier:

'#§\\(\\[a-z0-9-_\\]+\\)\.?\\(\\[a-z0-9-_\\]+\\)?#i'
//                                         here ___^

也不需要转义所有这些字符:

And there're no needs to escape all these characters:

'#§([a-z0-9_-]+)\.?([a-z0-9_-]+)?#i'

[a-z0-9 _] 可以重写 \w ,不需要 i 修饰符

[a-z0-9_] can be rewritten \w and there're no needs to i modifier

'#§([\w-]+)\.?([\w-]+)?#'

整个指令变为:

$buffer = preg_replace_callback(
  '#§([\w-]+)\.?([\w-]+)?#',
  function ($m) {
    return templ($m[1], $m[2]);
  },
  $buffer
);

这篇关于从preg_replace到preg_replace_callback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆