我试图从文本中删除撇号,但它并不真正工作。必须是很小的东西
$text = preg_replace('/''/', '', $text);
这就是我现在用来删除它的。我做错了什么?
有一系列这些来删除特殊字符,将它们转换为url并将它们存储在我的数据库中。然而,最近出现了一批带有'
非常感谢任何帮助。
尝试使用str_replace()
,它比preg_replace()
更快,因为它不使用正则表达式。
$text = str_replace("'", '', $text);
您可以使用这个regexp来删除撇号
$text = preg_replace('/(''|�*39;)/', '', $text);
也可以使用str_replace在执行html_entity_decode
后删除撇号$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;
}
来源:https://www.php.net/manual/en/function.str-replace.php