用 preg_replace 替换撇号时出现问题

13

我试图从文本中删除撇号,但实际上并没有成功。一定是哪里出了小问题。

$text = preg_replace('/\'/', '', $text);

这就是我现在正在使用的方法来移除它。我做错了什么吗?

这是一系列用于移除特殊字符并将它们转换为URL并存储到我的数据库中的步骤。但最近出现了一批带有'而不是'的内容。

非常感谢任何帮助。提前致谢。


5
为什么你在使用 preg_replace 而不是 str_replace 或者 strtr - Jon
对我来说很好用:http://www.ideone.com/ycu2E - 999999
1
它对我有效(尽管每个人都正确地指出preg_replace不合适)。也许你的输入有问题。 - mqsoh
我知道这是老问题,但重新阅读问题后,我发现代码是可以工作的真正的问题在于输入不是一个撇号。不知何故,HTML实体(和等)找到了它的输入方式。如果数据从Word文档或其他网站中剪切并粘贴,则很容易发生这种情况。因此,真正的解决方案是以下任何一个答案建议匹配撇号和代表撇号的HTML实体 - UncaAlby
6个回答

26

13

你可以使用这个正则表达式来移除单引号

$text = preg_replace('/(\'|&#0*39;)/', '', $text);

你也可以在使用html_entity_decode之后使用str_replace来移除撇号。

$text = str_replace("'","", html_entity_decode($text, ENT_QUOTES)); 

3
HTML实体边界-零填充。 - hakre
$text = preg_replace('/('|&#0*39;)/', '', $text); 这是唯一可行的解决方案! - dotMastaz

4

&#039代表撇号的HTML实体编码,即htmlspecialchars($text, ENT_QUOTES)。您可以检查这两种情况:

$text = "hey this is ' a couple of ' apostrophes with an encoding '";
$text = preg_replace('/&#0*39;|\'/', '', $text);

// outputs: hey this is  a bunch of  apostraphes
echo $text;

您也可以选择使用等效的str_replace()函数(该函数往往运行速度更快):

$text = "hey this is ' a couple of ' apostrophes with an encoding '";
$text = str_replace(array("'", "'"), '', $text);

// outputs: hey this is  a bunch of  apostraphes
echo $text;

3

除了其他答案,您可能还想检查Unicode表示形式吗?

$result = preg_replace('/([\'\x{0027}]|')/u', '', $subject);

1

对于这个问题,使用string_replace怎么样?这不需要正则表达式。

$sText = preg_match("'", "", $sText);

话虽如此,以下代码片段在5.3中按预期工作:

$text = "woo't";
$text = preg_replace('/\'/', '', $text);
echo $text; // woot

1

我曾经遇到过同样的问题,原因是文本来自 MS Word,它有自己奇怪的格式。

解决方案是先用 preg_replace 或 str_replace 将其和其他奇怪的字符替换为可以捕获的内容,下面的函数可以帮助实现这一点:

function msword_conversion($str)
{
    $str = str_replace(chr(130), ',', $str);    // baseline single quote
    $str = str_replace(chr(131), 'NLG', $str);  // florin
    $str = str_replace(chr(132), '"', $str);    // baseline double quote
    $str = str_replace(chr(133), '...', $str);  // ellipsis
    $str = str_replace(chr(134), '**', $str);   // dagger (a second footnote)
    $str = str_replace(chr(135), '***', $str);  // double dagger (a third footnote)
    $str = str_replace(chr(136), '^', $str);    // circumflex accent
    $str = str_replace(chr(137), 'o/oo', $str); // permile
    $str = str_replace(chr(138), 'Sh', $str);   // S Hacek
    $str = str_replace(chr(139), '<', $str);    // left single guillemet
// $str = str_replace(chr(140), 'OE', $str);   // OE ligature
    $str = str_replace(chr(145), "'", $str);    // left single quote
    $str = str_replace(chr(146), "'", $str);    // right single quote
// $str = str_replace(chr(147), '"', $str);    // left double quote
// $str = str_replace(chr(148), '"', $str);    // right double quote
    $str = str_replace(chr(149), '-', $str);    // bullet
    $str = str_replace(chr(150), '-–', $str);    // endash
    $str = str_replace(chr(151), '--', $str);   // emdash
// $str = str_replace(chr(152), '~', $str);    // tilde accent
// $str = str_replace(chr(153), '(TM)', $str); // trademark ligature
    $str = str_replace(chr(154), 'sh', $str);   // s Hacek
    $str = str_replace(chr(155), '>', $str);    // right single guillemet
// $str = str_replace(chr(156), 'oe', $str);   // oe ligature
    $str = str_replace(chr(159), 'Y', $str);    // Y Dieresis
    $str = str_replace('°C', '&deg;C', $str);    // Celcius is used quite a lot so it makes sense to add this in
    $str = str_replace('£', '&pound;', $str);
    $str = str_replace("'", "'", $str);
    $str = str_replace('"', '"', $str);
    $str = str_replace('–', '&ndash;', $str);

    return $str;
} 

来源:https://www.php.net/manual/zh/function.str-replace.php


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接