PHP正则表达式接受日语和英语 [英] PHP regex to accept Japanese and english languages
问题描述
我正在尝试创建一个正则表达式以仅过滤英语和日语中的字母或数字.这就是我尝试过的,
I am trying to create a regex to filter only alphabets or numbers from English and Japanese languages. This is what I have tried,
preg_match('/(?![\n\r])[\x00-\x1F\x80-\xFF][^\x4e00-\x9fa0)]/u', $value)
但是我没有得到想要的结果.我可能做错了什么?
But I am not getting the desired result. What might I be doing wrong?
推荐答案
您应使用 unicode character properties
您也可以在此网站上查看,其中包含其他正则表达式示例 http://www.localizingjapan.com/blog/2012/01/20/regular-expressions-for-japanese-text/
You should use unicode character properties
Also you may have a look on this website which contains some other regex examples http://www.localizingjapan.com/blog/2012/01/20/regular-expressions-for-japanese-text/
基于@ÁlvaroGonzález通知的更新的字符列表有关三个字母.
此正则表达式应该达到您的期望:
this regex should do what you expect :
preg_match('/[\p{L}\p{N}\p{Katakana}\p{Hiragana}\p{Han}]+/u', $value)
\p{L}
将匹配任何字母,\p{N}
任何数字,并且\p{Katakana}
将匹配任何片假名字符等...
\p{L}
will match any letter, \p{N}
any number and \p{Katakana}
will match any Katakana char etc...
如果您不匹配单个单词,则可能需要在可接受的字符中添加单词定界符
You may need to add word delimiters into the accepted characters if you are not matching single words
这篇关于PHP正则表达式接受日语和英语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!