For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > November 2005 > Regular expression









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 Regular expression
Willem-Jan

2005-11-29, 7:55 am

Hi all,

I want to check the input by a form field by PHP. I only want to allow
capital or small letters, numbers, a - and a space. Now I use
"[^A-Za-z0-9-]" . What do I need to add to allow spaces?

Thanx!
WJ
Oli Filth

2005-11-29, 7:55 am

Willem-Jan wrote:
> Hi all,
>
> I want to check the input by a form field by PHP. I only want to allow
> capital or small letters, numbers, a - and a space. Now I use
> "[^A-Za-z0-9-]" . What do I need to add to allow spaces?
>


\s


--
Oli

Hilarion

2005-11-29, 6:57 pm

> I want to check the input by a form field by PHP. I only want to allow
> capital or small letters, numbers, a - and a space. Now I use
> "[^A-Za-z0-9-]" . What do I need to add to allow spaces?



Shouldn't it have the "^" sign before the "["? Now it allows
everything beside letters, digits and a minus sign. You
should also add "*" after the "]" to allow not only one valud
character, but any number of it, and add "$" sign at the end
to prevent matching text that starts with valid characters,
but then contains invalido ones.
You can add space by prefixing it with backslash.

If you are using single quotes for the expression, then it should
be:

'^[\ A-Za-z0-9-]*$'

If you are using double quotes, then the backslash should be
escaped (to make it a part of the string, and not a PHP way
of escaping special character) by prefixing it with another
one (and you probably should escape the "$" sign too):

"^[\\ A-Za-z0-9-]*\$"

As you can see it's easier to use single quotes around regular
expressions.

Hilarion
Sponsored Links







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

Copyright 2008 codecomments.com