views:

61

answers:

3

Hi, 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

The html output is

I want to remove the – the dash

I want to do this:

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

I've tried to parse the string with html_entity_decode, to parse the character to remove with htmlspecialchars,but without any results.

What I'm 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 works. Basically the problem is that in the DB the dashes are stored as "minus" (I enter the value with the minus key) but for a strange reason the output is &ndash ;

I'm running on Wordpress and the charset is UTF-8, the same for the DB collation.

A: 

Try this:

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

Or:

$new_string = str_replace(html_entity_decode('–'),'',$string);

It is basically same as:

$new_string = str_replace ('-','',$string);
Sarfraz
I've tried all of this but with no fortune.I really can't understand.In the db the dashed as stored as -, not in "
Pennywise83
+1  A: 

There's – (–) and there's the minus sign (-). Make sure you are not trying to replace the wrong character.

Álvaro G. Vicario
Pennywise83
If you mean a literal `–`, first sAc's solution should apply. Are you escaping output with `htmlentities()`? Do you replace before or after escaping?
Álvaro G. Vicario
No to both question. It's very strange. I'm going to update the OP with the exact code I have.
Pennywise83
+1  A: 

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

EDIT: 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);

ircmaxell
With your first str_replace all things works! Now I'm curious why I output ndash instead of minus... I'll take a look at the hex strings.Thank you!
Pennywise83
Well, then the problem was that your source code file was not actually UTF-8 (The character you thought was ndash was something else)... If you use the UTF-8 character set, always add the `'UTF-8'` parameter to `html(_entity_decode|entities|specialchars)`
ircmaxell