1

Im trying to get this thing working where i accept all unicode letters, so can cover languages like urdu and other special letters from around the world.

Tried this :

/^[\p{L}\p{Zs}\p{N}]+$/uix

But it gets me no where what the heck am i doing wrong? even tried some of the regex tools and just cant get this to work in any way.

Got it working i found out it was my htmlentities messing with the result but is it safe to use $_POST with preg_match ?

!preg_match("/^[\\p{L}\\p{Zs}\\p{N}\\p{M}]+$/u", $_POST['firstname'])
Benzon
  • 119
  • 6
  • Are your pages saved with UTF8 encoding? Also, add `\p{M}` to the character class – Wiktor Stribiżew Jan 27 '16 at 07:24
  • Yep text/x-php; charset=utf-8 it what linux gives me when i check it – Benzon Jan 27 '16 at 07:33
  • Try `'/^[\p{L}\p{M}\p{Zs}\p{N}]+$/u'`. You do not need to double the backslash when using a single quoted literal. – Wiktor Stribiżew Jan 27 '16 at 07:53
  • See [this question](http://stackoverflow.com/questions/2622856/is-preg-match-safe-enaught-in-input-satinization) about using POST in preg_match (and [this article](https://www.dreamhost.com/blog/2013/05/22/php-security-user-validation-and-sanitization-for-the-beginner/) looks interesting, too). – Wiktor Stribiżew Jan 27 '16 at 07:58
  • "An invalid subject will [...] match nothing"— http://php.net/manual/en/reference.pcre.pattern.modifiers.php. Are you sure, that the string that you feed into the regexp is valid UTF-8? Also, as Wiktor says, `\pM` and `\pN` are useful additions, maybe `\pP` for punctuation, too. – Boldewyn Jan 27 '16 at 08:11

1 Answers1

1

You can use

'/^[\p{L}\p{M}\p{Zs}\p{N}]+$/u'

Note that Urdu and a lot of other languages have diacritics and \p{M} matches them.

You do not need to double the backslash when using a single quoted literal.

With POST, it seems you just need to make sure the value is set and you use a string.

if ( isset( $_POST[ 'firstname' ] ) ) {
  $name = strip_tags( trim( $_POST[ 'firstname' ] ) );
}

See this article.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563