【问题标题】:preg_replace remove special characters, ampersand changes to numberspreg_replace 删除特殊字符,与号更改为数字
【发布时间】:2016-01-12 18:16:13
【问题描述】:

我正在使用预赛:

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

是的,我使用 Wordpress,但由于这应该只是一个字符串,我想我可以在这里问它。每当我使用这个预匹配时,$titleClass 的输出会用 038 替换 & 符号,知道如何删除它吗?在像“|”这样的符号上效果很好。

【问题讨论】:

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


    【解决方案1】:

    您的 & 符号似乎已转换为 htmlentity &038;。完成替换后,您的脚本会删除除数字和拉丁字符之外的所有字符。所以&038; 被转换为038

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

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

    【讨论】:

    • 奇怪的是,如果该方法本身是回显的,它只会显示 & 正常
    • 您是查看页面源还是在浏览器中查看?浏览器会将它显示为 &,而在源代码中它应该是 &038;
    • 解码功能有效,谢谢!我在浏览器中检查确实是我的错。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多