gpt4 book ai didi

php - 警告:preg_replace_callback():需要参数2

转载 作者:行者123 更新时间:2023-12-04 21:59:41 32 4
gpt4 key购买 nike

我刚刚将服务器上的PHP从php 5更新到了php 7,并且收到以下警告:


警告:preg_replace_callback()[function.preg-replace-callback0]:要求参数2'chr(\ 1)'为有效的回调

警告:preg_replace_callback()[function.preg-replace-callback0]:要求参数2'chr(0x \ 1)'为有效的回调

警告:preg_replace_callback()[function.preg-replace-callback0]:要求参数2'chr(\ 1)'为有效的回调

警告:preg_replace_callback()[function.preg-replace-callback0]:要求参数2'chr(0x \ 1)'为有效的回调


这是PHP代码:

private function _decode( $source )
{
$source = html_entity_decode($source, ENT_QUOTES, 'UTF-8');
$source = preg_replace_callback('/&#(\d+);/me',"chr(\\1)", $source);
$source = preg_replace_callback('/&#x([a-f0-9]+);/mei',"chr(0x\\1)", $source);

return $source;
}


警告来自:

$source = preg_replace_callback('/&#x([a-f0-9]+);/mei',"chr(0x\\1)", $source);


我该如何解决?

最佳答案

the PHP 7.0 migration guide中所述,不再支持/e修饰符(PREG_REPLACE_EVAL)。您需要使用可调用函数,而不是将被视为函数的字符串。在您的情况下,将字符串函数chr(0x\\1)替换为Closure:

$source = preg_replace_callback(
'/&#x([a-f0-9]+);/mi',
fn($m) => chr(hexdec('0x'.$m[1])),
$source
);


内联字符串替换 \\1以产生有效的PHP十六进制(如 0x21)在可调用对象中不再起作用:您需要 hexdec调用才能实现相同目的。

See it in action on 3v4l.org.



如果您还没有短封闭的PHP 7.4,则需要这样写:

$source = preg_replace_callback(
'/&#x([a-f0-9]+);/mi',
function ($m) { return chr(hexdec('0x'.$m[1])); }, // Now a Closure
$source
);

关于php - 警告:preg_replace_callback():需要参数2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44980924/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com