php 无法用 Preg_Replace 替换撇号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7822631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trouble Replacing Apostrophe with Preg_Replace
提问by NotJay
I am attempting to remove apostrophes from text and it isn't really working. It's got to be something small.
我试图从文本中删除撇号,但它并没有真正起作用。一定是个小东西。
$text = preg_replace('/\'/', '', $text);
That's what I am using right now to remove it. What am I doing wrong?
这就是我现在用来删除它的方法。我究竟做错了什么?
There is a series of these to remove special characters to turn them into urls and store them in my database. However, a recent batch appeared with a ' where the ' was.
有一系列这些可以删除特殊字符以将它们转换为 url 并将它们存储在我的数据库中。然而,最近一批出现了' '在哪里。
Any help is greatly appreciated. Thank you in advance.
任何帮助是极大的赞赏。先感谢您。
回答by 472084
Have a go using str_replace()
, it's quicker than preg_replace()
since it doesn't use regular expressions.
试一试str_replace()
,它比preg_replace()
不使用正则表达式要快。
$text = str_replace("'", '', $text);
回答by Aurimas Li?kus
you can use this regexp to remove apostrophes
您可以使用此正则表达式删除撇号
$text = preg_replace('/(\'|�*39;)/', '', $text);
also you can use str_replace to remove apostrophes after doing html_entity_decode
您也可以在执行 html_entity_decode 后使用 str_replace 删除撇号
$text = str_replace("'","", html_entity_decode($text, ENT_QUOTES));
回答by Corey Ballou
' represents the HTML entity encoding of an apostrophe, i.e. htmlspecialchars($text, ENT_QUOTES)
. You can check for both cases:
' 表示撇号的 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;
You can also stick with the str_replace()
equivalent (tends to run faster):
你也可以坚持使用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;
回答by FailedDev
In addition to the other answers, you may want to check for the unicoderepresentation too?
除了其他答案之外,您可能还想检查unicode表示吗?
$result = preg_replace('/([\'\x{0027}]|')/u', '', $subject);
回答by Robert Sinclair
Had the same problem and it was stemming from the fact that text was being pasted from MS word which has it's own strange formatting
有同样的问题,这是因为文本是从 MS word 粘贴的,它有自己奇怪的格式
Solution was to first replace it and other weird characters with something which can be then captured by preg_replace or str_replace, the func below will help with that:
解决方案是首先用可以被 preg_replace 或 str_replace 捕获的东西替换它和其他奇怪的字符,下面的 func 将对此有所帮助:
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;
}
Source: https://www.php.net/manual/en/function.str-replace.php
回答by Wesley van Opdorp
How about using string_replacefor that, this doesn't require a regular expression.
为此使用string_replace怎么样,这不需要正则表达式。
$sText = preg_match("'", "", $sText);
That being said, the following snippet works as supposed in 5.3:
话虽如此,以下代码段在 5.3 中按预期工作:
$text = "woo't";
$text = preg_replace('/\'/', '', $text);
echo $text; // woot