Can't remove special characters with str_replace

后端 未结 8 672
北荒
北荒 2020-12-07 04:00

I have a very trivial problem with str_replace.

I have a string with the En Dash character ( - ) like this:

I want to remove - the dash
8条回答
  •  遥遥无期
    2020-12-07 04:41

    try something like this:

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

    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);
    }
    

    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 . Anything else means a different character...

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

提交回复
热议问题