Home » Php » Weird dash character in PHP

Weird dash character in PHP

Posted by: admin July 12, 2020 Leave a comment


I have a weird dash in my text, which isn’t being detected in a str_replace.

Here is an example:

Sun: 10:00 – 3:00pm

I don’t know if the dash will show up on here.. but when it is inserted into my table, it is like a square box with the characters 0096

It looks a lot like an – – when viewing the source, there is no special characters, just the dash.

str_replace('–', '', $var);

The above replace doesn’t seem to catch it, has anyone else had this trouble before?

How to&Answers:

That’s an en dash. In php, the most portable way to get it is with html_entity_decode:

$endash = html_entity_decode('–', ENT_COMPAT, 'UTF-8');
echo str_replace($endash, '(en dash)', 'Sun: 10:00 – 3:00pm');

Note that this only works if your website encoding is UTF-8 and your editor encoding(or the encoding of the third argument to str_replace) is as well. If you use another encoding (and you should use the same both for website and editor), replace the third parameter of html_entity_decode with its name.


For an alternative, if other answers don’t work for you, like in my case, this works for me.

$title = "Hunting, Tactical & Outdoor Optics eCommerce Store ΓÇô $595,000 ΓÇö SOLD";
$title = str_replace(html_entity_decode('–', ENT_COMPAT, 'UTF-8'), '-', $title);
$title = str_replace(html_entity_decode('—', ENT_COMPAT, 'UTF-8'), '-', $title);


En - Dash we’re used to seeing
Em Dash that we’re supposed to use most of the time where use “-“.
Basically, it’s a grammar thing.

Read up on Wikipedia: http://en.wikipedia.org/wiki/Dash

On a mac (with US Keyboard Layout) I get it hitting Alt+- , same as you get _ with shift+_

You don’t need any special treatement to it as it were some chinese symbol. It is a valid character. Treat it as such: str_replace('–', 'em dash');.


jay suggested

$str = str_replace(chr(150), '-–', $str);    // endash
$str = str_replace(chr(151), '--', $str);   // emdash