How to remove special characters and keep letters

2020-03-26 06:15发布

I know this should remove any characters from string and keep only numbers and ENGLISH letters.

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

but I wish to remove any special characters and keep any letter of any language like Arabic or Japanese.

2条回答
等我变得足够好
2楼-- · 2020-03-26 06:35

Probably this will work for you:

$repl = preg_replace('/[^\w\s]+/u','' ,$txtbefore);

This will remove all non-word and non-space characters from your text. /u flag is there for unicode support.

查看更多
家丑人穷心不美
3楼-- · 2020-03-26 06:47

You can use the \p{L} pattern to match any letter and \p{N} to much any numeric character. Also you should use u modifier like this: /\p{L}+/u

Your final regex may look like: /[^\p{L}\p{N}]/u

Also be sure to check this question:

Regular expression \p{L} and \p{N}

查看更多
登录 后发表回答