Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

preg_match('/^p{L}[p{L} _.-]+$/u', $username)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...