删除标点符号、符号、变音符号、特殊字符的最佳方法是什么?

发布于 2024-10-13 06:17:49 字数 495 浏览 3 评论 0原文

我使用这些代码行来删除所有标点符号、符号等,因为您可以在数组中看到它们,

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泼猴你往哪里跑 2024-10-20 06:17:49

根据您的贪心程度,您可以执行以下操作:

$pg_url = preg_replace("/[^a-zA-Z 0-9]+/", " ", $pg_url);

这将替换除字母、数字或空格之外的任何内容。

Depending on how greedy you'd like to be, you could do something like:

$pg_url = preg_replace("/[^a-zA-Z 0-9]+/", " ", $pg_url);

This will replace anything that isn't a letter, number or space.

暖心男生 2024-10-20 06:17:49

使用类:

preg_replace('/[^[:alpha:]]/', '', $input);

将删除当前设置的区域设置不认为是“字符”的任何内容。如果是标点符号,您试图消除它,则该类将是 [:punct:]

\W 表示“任何非单词字符”,与 \w 相反,后者包括下划线 (_ )。

Use classes:

preg_replace('/[^[:alpha:]]/', '', $input);

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 (_).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文