使用 Preg_Replace 替换撇号时遇到问题
我试图从文本中删除撇号,但它并没有真正起作用。一定是一件小事。
$text = preg_replace('/\'/', '', $text);
这就是我现在用来删除它的方法。我做错了什么?
有一系列的方法可以删除特殊字符,将它们转换为网址并将它们存储在我的数据库中。然而,最近一批出现了一个' ' 在哪里。
非常感谢任何帮助。先感谢您。
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.
Any help is greatly appreciated. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试使用
str_replace()
,它比preg_replace()
因为它不使用正则表达式。Have a go using
str_replace()
, it's quicker thanpreg_replace()
since it doesn't use regular expressions.您可以使用此正则表达式删除撇号,
也可以在执行 html_entity_decode 后使用 str_replace 删除撇号
you can use this regexp to remove apostrophes
also you can use str_replace to remove apostrophes after doing html_entity_decode
' 表示撇号的 HTML 实体编码,即
htmlspecialchars($text, ENT_QUOTES)
。您可以检查这两种情况:您也可以坚持使用
str_replace()
等效项(往往运行得更快):' represents the HTML entity encoding of an apostrophe, i.e.
htmlspecialchars($text, ENT_QUOTES)
. You can check for both cases:You can also stick with the
str_replace()
equivalent (tends to run faster):除了其他答案之外,您可能还想检查 unicode 表示形式?
In addition to the other answers, you may want to check for the unicode representation too?
如何使用 string_replace 来实现这一点,这不需要正则表达式。
话虽这么说,以下代码片段按照 5.3 中的假设工作:
How about using string_replace for that, this doesn't require a regular expression.
That being said, the following snippet works as supposed in 5.3:
遇到了同样的问题,这是由于文本是从 MS Word 粘贴的,它有自己奇怪的格式解决
方案是首先用可以由 preg_replace 或 str_replace 捕获的东西替换它和其他奇怪的字符,下面的函数将对此有所帮助:
来源:https://www.php.net/manual/en/function.str-replace.php
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
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:
Source: https://www.php.net/manual/en/function.str-replace.php