PHP - replace all non-alphanumeric chars for all languages supported

二次信任 提交于 2021-02-07 17:54:29

问题


Hi i'm actually trying replacing all the NON-alphanumeric chars from a string like this:

mb_ereg_replace('/[^a-z0-9\s]+/i','-',$string);

first problem is it doesn't replaces chars like "." from the string.

Second i would like to add multybite support for all users languages to this method.

How can i do that?

Any help appriciated, thanks a lot.


回答1:


Try the following:

preg_replace('/[^\p{L}0-9\s]+/u', '-', $string);

When the u flag is used on a regular expression, \p{L} (and \p{Letter}) matches any character in any of the Unicode letter categories.




回答2:


It should replace . with -, you're probably mixing up your data in the first place.

As for the multi-byte support, add the u modifier and look into PCRE properties, namely \p{Letter}:

$replaced = preg_replace('~[^0-9\p{Letter}]+~iu', '-', $string);



回答3:


The shortest way is:

$result = preg_replace('~\P{Xan}++~u', '-', $string);

\p{Xan} contains numbers and letters in all languages, thus \P{Xan} contains all that is not a letter or a number.




回答4:


This expression does replace dots. For multibyte use u modifier (UTF-8).



来源:https://stackoverflow.com/questions/17115867/php-replace-all-non-alphanumeric-chars-for-all-languages-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!