Check characters of a variable

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
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Check characters of a variable

Post by SirChick »

Is there a way to check if a variable contains symbols? And then if it does produce an error?

example:

chee'se or ch{}#eese = error

but cheese would be ok.
Last edited by SirChick on Thu Oct 25, 2007 6:13 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yes and yes.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Solution would be regular expressions. Precise details not easily given as which characters are allowed a-z, 0-9, öäü ? If using UTF-8 you may also want to look at Multibyte String Functions
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

At this point, I can only recommend ctype_alnum().
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

wow - I've never even seen those functions before - nice! It would be wicked fast compared to my default regexp instinct.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Glad I'm not the only one who just learnt something :P
Post Reply