删除标点符号、符号、变音符号、特殊字符的最佳方法是什么?
我使用这些代码行来删除所有标点符号、符号等,因为您可以在数组中看到它们,
$pattern_page = array("+",",",".","-","'","\"","&","!","?",":",";","#","~","=","/","$","£","^","(",")","_","<",">");
$pg_url = str_replace($pattern_page, ' ', strtolower($pg_url));
但我想让它更简单,因为列出我想要在数组中删除的所有内容看起来很愚蠢,因为可能有是我想删除的其他一些特殊字符。
我想过使用下面的正则表达式,
$pg_url = preg_replace("/\W+/", " ", $pg_url);
但它不会删除下划线 - _
删除所有这些内容的最佳方法是什么?正则表达式可以做到这一点吗?
I use these lines of code to remove all punctuation marks, symbols, etc as you can see them in the array,
$pattern_page = array("+",",",".","-","'","\"","&","!","?",":",";","#","~","=","/","$","£","^","(",")","_","<",">");
$pg_url = str_replace($pattern_page, ' ', strtolower($pg_url));
but I want to make it simpler as it looks silly to list all the stuff I want to remove in the array as there might be some other special characters I want to remove.
I thought of using the regular expression below,
$pg_url = preg_replace("/\W+/", " ", $pg_url);
but it doesn't remove under-score - _
What is the best way to remove all these stuff? Can regular expression do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的贪心程度,您可以执行以下操作:
这将替换除字母、数字或空格之外的任何内容。
Depending on how greedy you'd like to be, you could do something like:
This will replace anything that isn't a letter, number or space.
使用类:
将删除当前设置的区域设置不认为是“字符”的任何内容。如果是标点符号,您试图消除它,则该类将是
[:punct:]
。\W
表示“任何非单词字符”,与\w
相反,后者包括下划线 (_
)。Use classes:
Would remove anything that's not considered a "character" by the currently set locale. If it's punctuation, you seek to eliminate, the class would be
[:punct:]
.\W
means "any non-word character" and is the opposite of\w
which includes underscores (_
).