且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法使用str_replace删除特殊字符

更新时间:2023-02-05 21:06:04

尝试如下操作:

str_replace(html_entity_decode('–', ENT_COMPAT, 'UTF-8'), '', $string);

我的猜测是,这实际上不是ndash,而是一个非常相似的角色.我建议拉出字符串中每个字符的字节值,以查看其外观:

My guess is it's not really an ndash, but a very similar character. I'd suggest pulling the byte values of each character in the string to see what it looks like:

function decodeString($str) {
    //Fix for mb overloading strlen option
    if (function_exists('mb_strlen')) { 
        $len = mb_strlen($str, '8bit');
    } else {
        $len = strlen($str);
    }
    $ret = '';
    for ($i = 0; $i < $len; $i++) {
        $ret .= dechex(ord($str[$i])).' ';
    }
    return trim($ret);
}

这会将字符串转换为单个字节编码(将其转换为十六进制字符串,例如48 65 6C 6C 6F(Hello).检查两种情况下的破折号实际上是相同的字符.如果看到"2D",其中的破折号是一个文字减号...如果看到三个字节的序列E2 80 93,那就是&ndash;.其他任何内容都表示一个不同的字符...

That'll convert the string into the individual byte encodings (turn it into a hex string like 48 65 6C 6C 6F (Hello). Check to see the dash in both cases is in fact the same character. If you see "2D" where the dash is, that's a literal minus sign... If you see the three byte sequence E2 80 93, that's &ndash;. Anything else means a different character...

而且,如果您看到26 6E 64 61 73 68 3B表示文字&ndash;,那么您就需要执行str_replace('&ndash;', '', $str);

And if you see 26 6E 64 61 73 68 3B that mens a literal &ndash;, so you'd need to do str_replace('&ndash;', '', $str);