php 正则表达式:去除非字母数字或标点符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3050352/
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
Regex: Strip non alpha numeric or punctuation
提问by Tedd
How can I use PHP to strip out all characters that are NOT alpha, numeric, space, or puncutation?
如何使用 PHP 去除所有非字母、数字、空格或标点符号的字符?
I've tried the following, but it strip punctuation.
我尝试了以下方法,但它去掉了标点符号。
preg_replace("/[^a-zA-Z0-9\s]/", "", $str);
回答by Matthew Flaschen
preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", $str);
Example:
例子:
php > echo preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", "?f?oo?. ba?r!");
foo. bar!
\p{P}matches all Unicode punctuation characters (see Unicode character properties). If you only want to allow specific punctuation, simply add them to the negated character class. E.g:
\p{P}匹配所有 Unicode 标点字符(请参阅 Unicode字符属性)。如果您只想允许特定的标点符号,只需将它们添加到否定字符类。例如:
preg_replace("/[^a-zA-Z0-9\s.?!]/", "", $str);
回答by cletus
You're going to have to list the punctuation explicitly as there is no shorthand for that (eg \sis shorthand for white space characters).
您将不得不明确列出标点符号,因为它没有简写(例如\s,空白字符的简写)。
preg_replace('/[^a-zA-Z0-9\s\-=+\|!@#$%^&*()`~\[\]{};:\'",<.>\/?]/', '', $str);
回答by MojganK
$str = trim($str);
$str = trim($str, "\x00..\x1F");
$str = str_replace(array( ""","'","&","<",">"),' ',$str);
$str = preg_replace('/[^0-9a-zA-Z-]/', ' ', $str);
$str = preg_replace('/\s\s+/', ' ', $str);
$str = trim($str);
$str = preg_replace('/[ ]/', '-', $str);
Hope this helps.
希望这可以帮助。

