Accent characters in regexp

This is a discussion on Accent characters in regexp within the alt.comp.lang.php forums, part of the PHP Programming Forums category; So I had this 'nifty' piece of regexp that would check for valid names. 'Had' I say untill I discovered ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-06-2007
Laiverd.COM
 
Posts: n/a
Default Accent characters in regexp

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



Reply With Quote
  #2 (permalink)  
Old 04-06-2007
Janwillem Borleffs
 
Posts: n/a
Default Re: Accent characters in regexp

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




Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:19 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0