preg_replace 去除特殊字符,& 符号更改为数字
preg_replace remove special characters, ampersand changes to numbers
我正在使用预匹配:
$titleClass = str_replace(' ', '-', get_the_title());
$titleClass = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($titleClass));
$titleClass = preg_replace('/-+/', '-', $titleClass);
我使用 Wordpress 是的,但由于这应该只是一个字符串,我想我可以在这里问一下。每当我使用这个 pregmatch 时,$titleClass
的输出都会用 038 替换&符号,知道如何删除它吗?在像“|”这样的符号上它完美运行。
您的 & 符号似乎已转换为 htmlentity &038;
。完成替换后,您的脚本会删除除数字和拉丁字符之外的所有字符。所以 &038;
转换为 038
.
您可以在进行任何替换之前通过输出 get_the_title()
函数的结果来检查它是否为真。
为了删除它,您可以在进行替换之前使用 html_entity_decode() 函数:
http://php.net/manual/en/function.html-entity-decode.php
我正在使用预匹配:
$titleClass = str_replace(' ', '-', get_the_title());
$titleClass = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($titleClass));
$titleClass = preg_replace('/-+/', '-', $titleClass);
我使用 Wordpress 是的,但由于这应该只是一个字符串,我想我可以在这里问一下。每当我使用这个 pregmatch 时,$titleClass
的输出都会用 038 替换&符号,知道如何删除它吗?在像“|”这样的符号上它完美运行。
您的 & 符号似乎已转换为 htmlentity &038;
。完成替换后,您的脚本会删除除数字和拉丁字符之外的所有字符。所以 &038;
转换为 038
.
您可以在进行任何替换之前通过输出 get_the_title()
函数的结果来检查它是否为真。
为了删除它,您可以在进行替换之前使用 html_entity_decode() 函数: http://php.net/manual/en/function.html-entity-decode.php