PHP替换EM Dash REGEX

时间:2017-08-18 03:41:55

标签: php regex

我正在尝试替换此字符http://www.fileformat.info/info/unicode/char/2014/index.htm 有一个常规冲刺,这是我现在似乎无法让它工作?

$dataold = "9am – 5pm"; // from a ms word doc

$data = mb_ereg_replace("[\xE2 \x80 \x94]", " - ", $dataold);

print_r($data);

3 个答案:

答案 0 :(得分:1)

为什么要使用八进制unicode格式呢?为什么不......

替换N-Dash

$dataold = "9am – 5pm"; // from a ms word doc
$data = mb_ereg_replace(" – ", " - ", $dataold);
print_r($data);

替换M-Dash

$dataold = "9am — 5pm"; // from a ms word doc
$data = mb_ereg_replace(" — ", " - ", $dataold);
print_r($data);

您的原始代码工作正常,但示例文本字符串有一个N-dash并且您正在测试M-dash。 (并且您的RegEx中有额外的空格)。试试这个......

$dataold = "9am — 5pm"; // from a ms word doc
$data = mb_ereg_replace("[\xE2\x80\x94]", " - ", $dataold);
print_r($data);

答案 1 :(得分:0)

试试这个:

$dataold = "9am – 5pm"; // from a ms word doc
$data = mb_ereg_replace("/ \\x{2014} /u", " - ", $dataold);
print_r($data);

答案 2 :(得分:0)

我让它工作但是 - 直接从您粘贴的页面复制字符(从您的代码复制不起作用)。

<?php
$dataold = "9am — 5pm"; // from a ms word doc
$data = mb_ereg_replace("\x{2014}", "dash", $dataold);
print_r($data); // output = 9am dash 5pm

演示:https://3v4l.org/9Lkpr