Weird dash character in PHP

前端 未结 4 1920
忘了有多久
忘了有多久 2020-12-19 07:23

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

相关标签:
4条回答
  • 2020-12-19 07:39

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

    0 讨论(0)
  • 2020-12-19 07:47

    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
    
    0 讨论(0)
  • 2020-12-19 07:49

    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.

    0 讨论(0)
  • 2020-12-19 07:52

    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);
    
    0 讨论(0)
提交回复
热议问题