Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 988
itsme Avatar asked Dec 12 '22 14:12

itsme


1 Answers

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.

like image 105
Andrew Clark Avatar answered Feb 15 '23 09:02

Andrew Clark