lopes_andre Posted March 7, 2010 Share Posted March 7, 2010 Hi, I need to validate international alphabetic characters. I need to get TRUE with characters like "ç", "é" and others, but is not working. How can I validate characters with accents? I have this: function alpha($str) { return ( ! preg_match("/^([*A-Za-zá-úÁ=Ú.\s*])+$/i", $str)) ? FALSE : TRUE; } Someone can give me a clue on how to validate characters with accents? Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/ Share on other sites More sharing options...
Mchl Posted March 7, 2010 Share Posted March 7, 2010 Try \w Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1022633 Share on other sites More sharing options...
lopes_andre Posted March 7, 2010 Author Share Posted March 7, 2010 Hi thanks for the reply. Can you explain more detailed? Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1022654 Share on other sites More sharing options...
Mchl Posted March 7, 2010 Share Posted March 7, 2010 Just like \s matches any white space, \w matches any 'word' character. This means all letters and 0-9 var_dump(preg_match('/\w/','Á')); // int(1) Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1022656 Share on other sites More sharing options...
salathe Posted March 7, 2010 Share Posted March 7, 2010 \w is not to be relied on for this kind of problem as precisely what it matches varies between different locales in use. I'm sure there have been threads discussing exactly that topic which might be worth a search. By "international alphabetic characters" do you mean absolutely any letter character at all? If so then \pL (make sure to use the u modifier) will likely be sufficient. If you only want accented latin characters, then \pL would be overkill. So it depends what precisely you are wanting to match. Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1022662 Share on other sites More sharing options...
lopes_andre Posted March 8, 2010 Author Share Posted March 8, 2010 Hi, I'm trying to get this working, but without success. I'm trying to validade "André" as TRUE, but gives me FALSE Here is the code: function alpha_international($str){ return ( ! preg_match("/^[\pL]+$/", $str)) ? FALSE : TRUE; } Someone can give me a clue? Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023208 Share on other sites More sharing options...
salathe Posted March 8, 2010 Share Posted March 8, 2010 I guess you missed the side-comment in my post (make sure to use the u modifier) as that is what is missing. With that appended to your regex pattern, you're good to go. Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023214 Share on other sites More sharing options...
lopes_andre Posted March 8, 2010 Author Share Posted March 8, 2010 Thanks for the reply. How can I put the "u" modifier? Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023217 Share on other sites More sharing options...
Mchl Posted March 8, 2010 Share Posted March 8, 2010 /[\pL]+/ seems to be working, but really I've no experience with unicode regex to say how good it is. BTW: return ( ! preg_match($pattern, $str)) ? FALSE : TRUE; and return (bool)preg_match($pattern, $str); do same thing Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023219 Share on other sites More sharing options...
lopes_andre Posted March 8, 2010 Author Share Posted March 8, 2010 Hi, thanks for the reply. Now it is OK if I write "André" but gives me OK also if I write "André-", "-" is not from the alphabet. How can I solve this? Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023224 Share on other sites More sharing options...
salathe Posted March 8, 2010 Share Posted March 8, 2010 Here's how I would do it, compare the function with yours to see the differences. function alpha_international($subject) { return (bool) preg_match('/^\pL+$/Du', $subject); } Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023235 Share on other sites More sharing options...
lopes_andre Posted March 8, 2010 Author Share Posted March 8, 2010 Here's how I would do it, compare the function with yours to see the differences. function alpha_international($subject) { return (bool) preg_match('/^\pL+$/Du', $subject); } Thanks for the reply. "André" is now OK. I need also to recognize Spaces as OK, to "André Lopes" be recognized as OK. Sorry for my questions. I know I need to study Regex... Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023255 Share on other sites More sharing options...
salathe Posted March 9, 2010 Share Posted March 9, 2010 Is there anything other than spaces that you need to allow? Just one space, new lines, tabs? Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023468 Share on other sites More sharing options...
lopes_andre Posted March 9, 2010 Author Share Posted March 9, 2010 Hi, Thanks for the reply. I only need Spaces between words. Thanks for your patience. Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023506 Share on other sites More sharing options...
salathe Posted March 9, 2010 Share Posted March 9, 2010 Spaces plural or just one, or?.. Sorry if this sounds like 20 questions but you need to be precise. This will match multiple words (at least one word; consisting only of 'letters') separated by a single space: /^\pL+(?: \pL+)*$/Du Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023529 Share on other sites More sharing options...
lopes_andre Posted March 9, 2010 Author Share Posted March 9, 2010 Spaces plural or just one, or?.. Sorry if this sounds like 20 questions but you need to be precise. This will match multiple words (at least one word; consisting only of 'letters') separated by a single space: /^\pL+(?: \pL+)*$/Du Hi salathe, No more needed. It does everything I need. Thanks for your help and patience. Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/194390-how-to-validade-international-alphabetic-characters/#findComment-1023789 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.