在PHP中使用preg_match匹配UTF字符:(* UTF8)在Windows上有效,但在Linux上无效 [英] 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);
在本地测试(使用WAMP的Windows 7)中,这将允许使用UTF字符(例如é或ñ)的用户名.但是,当我在实际托管站点的服务器上进行测试时,会收到以下警告:
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:
警告:preg_match()[function.preg-match]:编译失败:(* VERB)在第12行的/home/sites/vgmusic.com/test/Core/Impl/FormElementValidator.php中的偏移5处无法识别
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
我也在本地Ubuntu安装上尝试过此操作,并得到了相同的错误.实际上,我只在本地开发环境中看到了这项工作.有没有办法允许特殊字符适用于所有操作系统?
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?
推荐答案
通过 Unicode字符属性:
preg_match('/^\p{L}[\p{L} _.-]+$/u', $username)
这篇关于在PHP中使用preg_match匹配UTF字符:(* UTF8)在Windows上有效,但在Linux上无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!