我试图从文本中删除撇号,但实际上并没有成功。一定是哪里出了小问题。
$text = preg_replace('/\'/', '', $text);
这就是我现在正在使用的方法来移除它。我做错了什么吗?
这是一系列用于移除特殊字符并将它们转换为URL并存储到我的数据库中的步骤。但最近出现了一批带有'而不是'的内容。
非常感谢任何帮助。提前致谢。
我试图从文本中删除撇号,但实际上并没有成功。一定是哪里出了小问题。
$text = preg_replace('/\'/', '', $text);
这就是我现在正在使用的方法来移除它。我做错了什么吗?
这是一系列用于移除特殊字符并将它们转换为URL并存储到我的数据库中的步骤。但最近出现了一批带有'而不是'的内容。
非常感谢任何帮助。提前致谢。
你可以使用这个正则表达式来移除单引号
$text = preg_replace('/(\'|�*39;)/', '', $text);
你也可以在使用html_entity_decode之后使用str_replace来移除撇号。
$text = str_replace("'","", html_entity_decode($text, ENT_QUOTES));
'代表撇号的HTML实体编码,即htmlspecialchars($text, ENT_QUOTES)
。您可以检查这两种情况:
$text = "hey this is ' a couple of ' apostrophes with an encoding '";
$text = preg_replace('/�*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;
除了其他答案,您可能还想检查Unicode表示形式吗?
$result = preg_replace('/([\'\x{0027}]|')/u', '', $subject);
对于这个问题,使用string_replace怎么样?这不需要正则表达式。
$sText = preg_match("'", "", $sText);
话虽如此,以下代码片段在5.3中按预期工作:
$text = "woo't";
$text = preg_replace('/\'/', '', $text);
echo $text; // woot
我曾经遇到过同样的问题,原因是文本来自 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', '°C', $str); // Celcius is used quite a lot so it makes sense to add this in
$str = str_replace('£', '£', $str);
$str = str_replace("'", "'", $str);
$str = str_replace('"', '"', $str);
$str = str_replace('–', '–', $str);
return $str;
}
preg_replace
而不是str_replace
或者strtr
? - Jon