Validate a string with regex

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Validate a string with regex

Post by John Cartwright »

Code: Select all

^ї_a-zA-Z0-9-]+$
I know I'm stupid. I know I hate regex. How do you make it so the string can have whitespaces?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Pretty sure its:

Code: Select all

її:space:]]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm sorry, I didn't clarify the question.

How can I validate a string that is letters and numbers only, but will allow whitespaces.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Oh, you can use a ^ inside [] to not match a character, i,e, you can do:

Code: Select all

"tї^aeiou]+.*s"
Which matches "thanks", "this", "trappings", etc. So you can extend this principle to check to make sure that the characters that you dont want to be in a string arnt.

Hope that made sense...its late.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

^ї_a-zA-Z0-9ї:space:]-]+$
it matches any whitespace, not just a literal space
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

rehfeld wrote:

Code: Select all

^ї_a-zA-Z0-9ї:space:]-]+$
it matches any whitespace, not just a literal space
I should have known better... but hey, were all allowed to get stumped now and then..


thanks
Post Reply