Cannot delete special characters with str_replace

I have a very trivial problem with str_replace.

I have a line with the symbol En Dash (-) as follows:

I want to remove - the dash

Html output

I want to remove the – the dash

I want to do this:

$new_string = str_replace ('-','',$string);

I tried to parse a string with html_entity_decode in order to parse a character that needs to be removed using htmlspecialchars, but without any results.

What am I doing wrong?

-EDIT- This is the full code of my script:

$title = 'Super Mario Galaxy 2 - Debut Trailer'; // Fetched from the DB, in the DB the character is - (minus) not –

$new_title = str_replace(' - ', '', $title);
$new_title = str_replace(" - ", '', $title);
$new_title = str_replace(html_entity_decode('–'),'',$title);

No one is working. Basically the problem is that the dash is stored as a minus in the database (I entered the value using the minus key), but for some strange reason, the output is -

I work on Wordpress, and the encoding is UTF-8, the same for sorting a database.

+3
8

- :

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

, ndash, . , , :

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;. ...

EDIT: 26 6E 64 61 73 68 3B, &ndash;, str_replace('&ndash;', '', $str);

+9

, remove_filter( 'the_title', 'wptexturize' ); functions.php, str_replace - , "-";

+3

&ndash; (-) (-). , .

+1

. http://www.ascii.cl/htmlcodes.htm

        $arr1 = explode(",","0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F");
        $arr2 = explode(",","B,C,D,E,F");

        foreach($arr2 as $t1){
            foreach($arr1 as $t2){
                $val = $t1.$t2;
                $desc = str_replace(chr(hexdec($val)),"",$desc);
            }   
        }

        // if need removing individual value
        $desc = str_replace(chr(hexdec('A2')),"",$desc);
+1

:

$new_string = str_replace('&ndash;','',$string);

:

$new_string = str_replace(html_entity_decode('&ndash;'),'',$string);

, :

$new_string = str_replace ('-','',$string);
0

ndash:

$string = str_replace(chr(hexdec('3f')), '-', $string);
0

:

$string = str_replace("\x96", "-", $string);
0

For everyone who tried all of the above, but still had no joy, then it worked for me (from the WordPress function get_the_title())

$new_string = str_replace('&#8211;', 'or', $string);
0
source

Source: https://habr.com/ru/post/1752884/


All Articles