Allowing ' in full name

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Allowing ' in full name

Post by LonelyProgrammer »

Here's a regex I modified to allow ' in full name (well, really exotic names)

Code: Select all

 
$pattern = '#^[a-z\s\.\-\']+$#i'
 
However, it seems not to be failing, and the return is \'.

Is my regex wrong, or is magic quotes the source of my woe?
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Allowing ' in full name

Post by GeertDD »

Escape the quote inside the character class, otherwise it will result in a PHP parse error.
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Re: Allowing ' in full name

Post by LonelyProgrammer »

$pattern = '#^[a-z\s\.\-\']+$#i'

I swore I type this regex in the first message. I did escape the '
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Allowing ' in full name

Post by prometheuzz »

LonelyProgrammer wrote:$pattern = '#^[a-z\s\.\-\']+$#i'

I swore I type this regex in the first message. I did escape the '
Okay, but is there still a problem then? If so, can you post a string that is not matched while you think it should match?

Just a side note: you don't need to escape the . (dot) inside a character class and if you place the - (hyphen) at the start or at the end of the character class, you don't need to escape that either:

$pattern = '#^[a-z\s.\'-]+$#i';
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Re: Allowing ' in full name

Post by LonelyProgrammer »

Yes, ' are not being accepted in the text field, and it is being displayed as \' in the input box.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Allowing ' in full name

Post by prometheuzz »

LonelyProgrammer wrote:Yes, ' are not being accepted in the text field, and it is being displayed as \' in the input box.
I presume the "yes" is "yes there is still a problem". Since you didn't quote anyone, I am not sure about it. If this is correct, then again: please post the input that should be matched and the regex that is responsible for the "not matching" of that input string.
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Re: Allowing ' in full name

Post by LonelyProgrammer »

prometheuzz wrote:
LonelyProgrammer wrote:Yes, ' are not being accepted in the text field, and it is being displayed as \' in the input box.
I presume the "yes" is "yes there is still a problem". Since you didn't quote anyone, I am not sure about it. If this is correct, then again: please post the input that should be matched and the regex that is responsible for the "not matching" of that input string.
Hi,

Sorry for not making my post clear.

Example string to be matched: Abdul-'Adl
Regex used: $pattern = '#^[a-z\s.\'-]+$#i';

And I got the quote escaped back in the input text; So I am wondering if it's a magic quote issue after all
Attachments
invalid_name.jpg
invalid_name.jpg (9.98 KiB) Viewed 936 times
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Allowing ' in full name

Post by GeertDD »

Right, this is magic quotes madness. Nothing to do with regex.

http://www.sitepoint.com/blogs/2005/03/ ... headaches/
Post Reply