【问题标题】:Preg replace deprecated, attempting to fixPreg 替换已弃用,正在尝试修复
【发布时间】:2016-04-09 12:27:10
【问题描述】:

我刚刚升级到 PHP 7,并且一直在努力解决与不推荐使用的函数相关的错误,并取得了很大的成功。

遗憾的是,我在为“在交互式可折叠 javascript 事物中查看 php 数组”代码修复新的 preg 替换方法时遇到了麻烦。

以下代码:

function print_r_tree($data)
{

// capture the output of $this->print_r_tree
   $out = print_r($data, true);

 // replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
    $out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',"'\\1<a href=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'\\0'), 0, 7)).'\');\">\\2</a><div id=\"'.\$id.'\" style=\"display: none;\">'", $out);

  // replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
     $out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);

  // print the javascript function toggleDisplay() and then the transformed output
     echo '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n$out";

  }

生成此警告。 警告:preg_replace():不再支持 /e 修饰符,请改用 preg_replace_callback

删除第一个“preg_replace”中的“e”会破坏 javascript。我也尝试了一些 preg_replace_callback 的东西。

我一直在尝试使用此链接 Replace preg_replace() e modifier with preg_replace_callback 来帮助我了解问题所在,但我认为我的问题因 javascript 而变得复杂。

关于我的代码,我希望有人能够指导我完成这个过程?

提前致谢。

【问题讨论】:

标签: javascript php html regex deprecation-warning


【解决方案1】:

e 修饰符在您的第一个 $out 变量中。要转换它,您需要正确使用preg_replace_callback()

$out = preg_replace_callback('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iU', "callbackFunction", $out);

function callbackFunction($matches) {
     return "'".$matches[1]."<a href=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'".$matches[0]."'), 0, 7)).'\');\">".$matches[2]."</a><div id=\"'.\$id.'\" style=\"display: none;\">'";
}

看到在 preg_replace_callback 中我们用callbackFunction 定义了第二个参数,该字符串被解析以调用该函数,并传递一个带有匹配项的数组。所以替换是在callbackFunction() 函数中,匹配是matches[X]

更多信息:

http://php.net/manual/es/function.preg-replace-callback.php

祝你好运!

【讨论】:

    【解决方案2】:

    这是originalChristian's fix 的组合。

    function print_r_tree($data)
    {
        // capture the output of print_r
        $out = print_r($data, true);
    
        // replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
        $out = preg_replace_callback('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iU', 'print_r_tree_callback', $out);
    
        // replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
        $out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);
    
        // print the javascript function toggleDisplay() and then the transformed output
        return '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n$out";
    }
    
    function print_r_tree_callback($matches) {
        $id = substr(md5(rand().$matches[0]), 0, 7);
        return "$matches[1]<a href=\"javascript:toggleDisplay('$id');\">$matches[2]</a><div id='$id' style=\"display: none;\">";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 2016-10-15
      • 2015-06-25
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多