使用php preg_replace从utf-8编码的字符串

时间:2016-12-25 21:48:31

标签: php regex utf-8 preg-replace

我需要删除字符串中括号以外的标点符号。我想出了以下内容:

$clean = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $maybedirty );

这似乎工作正常,直到我意识到我需要通过一些utf-8编码字符(东欧)。虽然我找到了许多可能的解决方案的建议,但到目前为止我还没有使它们工作(或理解它们,或两者兼而有之)。所以问题是如何修改正则表达式以允许utf-8编码的字符。

1 个答案:

答案 0 :(得分:0)

$clean = preg_replace('/[^\w\s()]/', '', $maybedirty);

正则表达式说明:

[^\w\s()]

Match any single character NOT present in the list below «[^\w\s()]»
   A “word character” (Unicode; any letter or ideograph, any number, underscore) «\w»
   A “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «\s»
   A single character from the list “()” «()»