For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > April 2007 > Accent characters in regexp









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Accent characters in regexp
Laiverd.COM

2007-04-06, 7:58 am

So I had this 'nifty' piece of regexp that would check for valid names.
'Had' I say untill I discovered that it does not accept any accented
character like ë,é etc. I've been looking all over the place but cannot find
a clear answer as to the best solution for this problem. The only solution
that I get working sofar is by simply adding all possible accent characters
to the regexp, but I would assume there would be a more generic solution for
this ??

Anyway: below is what I have now, and it allows for a 'ë' in a string. The
only solution? Or is there indeed a better way?

PHP code:
define("VALID_NAME","^([A-Z]{1})([a-zA-Zë\s \-]+)([a-z])$");

Thanks for any light on this matter.

John



Janwillem Borleffs

2007-04-06, 6:58 pm

Laiverd.COM wrote:
> Anyway: below is what I have now, and it allows for a 'ë' in a
> string. The only solution? Or is there indeed a better way?
>


You can use the "w" character class, which matches both accented and normal
characters, as well as the underscore:

print preg_match('/\w/', 'ë'); // 1

As it appears that you only want to allow for spaces and hyphens, you could
do something like the following:

$word = 'éìë-';
print !preg_match('/_/', $word) &&
preg_match('/^[\w\s-]+$/', $word); // 1


JW




Aneredter15

2007-04-15, 1:51 am

Natalie Portman in anal action!
http://Natalie-Portman-anal-action....hp?movie=148803
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com