Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching UTF Characters with preg_match in PHP: (*UTF8) Works on Windows but not Linux

Tags:

regex

php

unicode

I have a simple regular expression to check a username:

preg_match('/(*UTF8)^[[:alnum:]]([[:alnum:]]|[ _.-])+$/i', $username);

In local testing (Windows 7 using WAMP), this will allow for usernames using UTF characters (such as é or ñ). However, when I move to test this on the server where the site will actually be hosted, I get the following warning:

Warning: preg_match() [function.preg-match]: Compilation failed: (*VERB) not recognized at offset 5 in /home/sites/vgmusic.com/test/Core/Impl/FormElementValidator.php on line 12

I have also tried this on a local Ubuntu installation and get the same error. In fact, I've only seen this work on my local development environment. Is there a way to allow for special characters that will work for all operating systems?

like image 518
Doctor Blue Avatar asked Feb 28 '10 16:02

Doctor Blue


People also ask

What is the use of Preg_match in PHP?

The preg_match() function returns whether a match was found in a string.

What does preg match return?

preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

What is PHP regex?

Regular expressions are commonly known as regex. These are nothing more than a pattern or a sequence of characters, which describe a special search pattern as text string. Regular expression allows you to search a specific string inside another string.


1 Answers

Try it by describing the characters by its Unicode character properties:

preg_match('/^\p{L}[\p{L} _.-]+$/u', $username)
like image 180
Gumbo Avatar answered Sep 20 '22 20:09

Gumbo