preg_replace_callback在服务器上不工作
问题描述:
preg_replace_callback();
不能在NGINX服务器上工作,但它在本地Apache服务器上工作。preg_replace_callback在服务器上不工作
,我想是不是在Apache/NGINX的错误,
,我将所有的电子邮件从字符串到图像,但它的显示如下一些错误。
Warning: preg_replace_callback(): Requires argument 2, 'encode_email', to be a valid callback in /home/abc/public_html/test.php on line 170
你可以看到我的代码是在这里:
<?php
$email_pattern = '/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/';
$text = 'my email is [email protected] and my second is [email protected]';
$html = preg_replace_callback($email_pattern, "encode_email", "$text");
echo $html;
function encode_email($matches){
return '<img src="image.php?id='. base64_encode($matches[0]) .'">';
}
?>
答
进行一些更改如下:
$text = 'my email is [email protected] and my second is [email protected]';
$email_pattern = '/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/';
$html = preg_replace_callback($email_pattern, function ($matches){
return '<img src="image.php?id='. base64_encode($matches[0]) .'">';
}, $text);
echo $html;
我希望它会工作。
这不太可能与nginx/apache的区别有关。这可能是两个系统上的PHP版本之间的差异吗? – Simba
@Simba你可能是对的,但两个服务器都有相同的PHP版本,但我认为这是一些逻辑或语法错误 –
你确定你不想使用'preg_replace()'?! – Rizier123