8

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?

pdlol
  • 389
  • 1
  • 5
  • 14
  • 1
    you should give us the string in [base64_encode](http://www.php.net/base64_encode) format, so that its preserved binary safe. This makes it ez for us to identify the character. – goat Jan 27 '12 at 00:32
  • Just asking the obvious but are you saving the return value of str_replace? I.e. `$var = str_replace('–', '', $var);` str_replace doesn't do operations directly on the string. – tangrs Jan 27 '12 at 00:32
  • What character set is your table using? – Jordan Running Jan 27 '12 at 00:37

4 Answers4

14

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.

phihag
  • 278,196
  • 72
  • 453
  • 469
1

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);
Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
0

http://php.net/manual/en/function.str-replace.php#102465 jay suggested

$str = str_replace(chr(150), '-–', $str);    // endash
$str = str_replace(chr(151), '--', $str);   // emdash
Aba
  • 584
  • 6
  • 11
0

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

pyronaur
  • 3,515
  • 6
  • 35
  • 52