Checking a string

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
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Checking a string

Post by dwfait »

Hi again. How would i check a variable if it contained any special characters? IE + ! " £ $ % ^ & * ( ) ; ' # ] [ , . / \ ¬ ` | ¦ { } ~ @ : ? > < - =.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

any type of Regular Expression would do the job well.

preg_match and all its forms.
ereg and all its forms would be what to look at.
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Post by dwfait »

umm..can you clarify and/or give me an example of the syntax? i cant find anything for it...
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

well, can you tell us what you are using this for?

Is this for a username sign-up script and you want to eliminate the possibilty of them using special characters?

This data could be helpful in assiting you. Depending on what your using this for, this regEx would only allow characters 0-9, a-z(capital or lowercase) to be allowed as well as a underscore and -.

Code: Select all

<?php
if (!ereg("^[_a-zA-Z0-9-]+$", $name)) {   
   echo "no special chars allowed.";
} else {
// do whateevr
}
?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

and if you wish to avoid regEx as much as possible (some people just hate them)

read: http://us2.php.net/manual/en/ref.ctype.php

But if this is not what your looking for, please let us know what you are wishing to accomplish
Post Reply