为什么这个preg_replace不起作用?正在删除HTML实体


Why does this preg_replace not work? Removing HTML entities

为什么这个preg_replace删除HTML实体不起作用?

// Remove all HTML entities
$text = preg_replace('/&[A-Za-z0-9]+?;/',' ', $text);

我只是想用空格替换所有HTML实体,比如(&##;、<等),但我似乎缺少了一些东西,因为它没有替换它们,我现在完全困惑了。


测试用例

代码:

// Remove all HTML entities
$title="&#9829;&#9829;&#9829; I like cats &#9829;&#9829;&#9829;";
echo "BEFORE : ".$title."'n";
$title2 = preg_replace('/&[A-Za-z0-9]+?;/e',' ', $title);
echo "AFTER : ".$title2."'n";

输出:

BEFORE : ♥♥♥ I like cats ♥♥♥
AFTER : ♥♥♥ I like cats ♥♥♥

PHP信息:

PHP版本:5.3.6-13 ubuntu 3.5

Regex库:绑定库启用

您缺少#

在您的preg_replace调用中应该是这个RegEx:

/&#[a-z'd]+;/i

尝试使用全局修饰符(e)