preg_replace删除特殊字符,&符号更改为数字

时间:2016-01-12 18:16:13

标签: php wordpress preg-replace str-replace ampersand

我正在使用pregmatch:

$titleClass = str_replace(' ', '-', get_the_title());
$titleClass = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($titleClass));
$titleClass = preg_replace('/-+/', '-', $titleClass);

我使用Wordpress是的,但因为这应该只是一个字符串,我想我可以在这里问。每当我使用这个pregmatch时,$titleClass的输出用038替换&符号,任何想法如何将其删除?在" |"等符号上它完美无缺。

1 个答案:

答案 0 :(得分:1)

看起来你的&符转换为htmlentity &038;。在进行替换后,脚本将删除除数字和拉丁字符之外的所有字符。因此&038;会转换为038

您可以在执行任何替换之前输出get_the_title()函数的结果来检查是否为真。

为了删除它,您可以在进行替换之前使用html_entity_decode()函数: http://php.net/manual/en/function.html-entity-decode.php

相关问题