Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - Accept international name characters in RegEx

I've always struggled with RegEx so forgive me if this may seem like an awful approach at tackling my problem.

When users are entering first and last names I started off just using the basic, check for upper and lower case, white space, apostrophes and hyphens

if (!preg_match("/^[a-zA-Zs'-]+$/", $name)) { // Error }

Now I realise this isn't the best since people could have things such as: Dr. Martin Luther King, Jr. (with comma's and fullstops). So I assume by changing it to this would make it slightly more effective.

if (!preg_match("/^[a-zA-Zs,.'-]+$/", $name)) { // Error }

I then saw a girls name I know on my Facebook who writes her name as Sian, which got me thinking of names which contain umlauts as well as say Japanese/Chinese/Korean/Russian characters too. So I started searching and found ways by writing each of these characters in there like so.

if (!preg_match("/^[a-zA-Zsàáa???èéê?ìí??òó????ùú?ü?y?????àá????èéê?ìí??òó????ùú?ü?Y?????????e ,.'-]+$/u", $first_name)) { // Error }

As you can imagine, it's extremely long winded and I'm pretty certain there is a much simpler RegEx which can achieve this. Like I've said, I've searched around but this is the best I can do.

So, what is a good way to check for upper and lower case characters, commas, full stops, apostrophes, hypens, umlauts, Latin, Japanese/Russian etc

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You can use an Unicode character class. pL covers pretty much all letter symbols.
http://php.net/manual/en/regexp.reference.unicode.php

 if (!preg_match("/^[a-zA-Zs,.'-pL]+$/u", $name))

See also http://www.regular-expressions.info/unicode.html, but beware that PHP/PCRE only understands the abbreviated class names.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...