tags:

views:

52

answers:

2

If I do, preg_replace('/[^a-zA-Z0-9\s-_]/','',$val) in a multi-lingual application, will it handle things like accented characters or russian characters? If not, how can I filter user input to only allow the above characters but with locale awareness?

thanks!

codecowboy.

A: 

No, it will only match the ASCII character A-Z. To match any letter/number in any language, you need to use the unicode properties of the regex engine:

preg_replace('/[^\p{L}\p{N}]/', '', $string);
soulmerge
A: 

Hi,

The only useful information I can find is from this page of the manual, which states :

A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place. For example, in the "fr" (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w.

Still, I wouldn't bet that it's working as you want...

But, to be sure :

  • maybe using unicode matching would be better
  • You'll probably have to try to be certain...

About unicode, the manual says this :

Matching characters by Unicode property is not fast, because PCRE has to search a structure that contains data for over fifteen thousand characters. That is why the traditional escape sequences such as \d and \w do not use Unicode properties in PCRE.

So, it might be a safer solution... curious about it, should I add ^^

Pascal MARTIN