Let's say you only want letters, digits and maybe an underscore in $string.
The following preg_match() will return "true" if there's anything else in there and "false" otherwise:
preg_match('/[_a-zA-Z0-9]/',$string)
Where the regexp means "anything that is not an underscore or a character in the ranges a to z, A to Z or 0 to 9". The whole thing between the [] is a "character class" and the ^ at the start supplies the "not".
If you want the reverse behaviour...
preg_match('/[_a-zA-Z0-9]*$/',$string)
This time the ^ means "the start of the string", the $ means "the end of the string", and the * means "zero or more of the stuff preceding" - in this case a character class. This regexp succeeds on any string that consists entirely of underscores, letters, and digits. Literally it says "starting from the beginning, match zero or more underscores, letters, or digits, (and only underscores, letters or digits) right on to the end of the string".
Look up more on the regular expression functions. I personally prefer the preg_-named ones, but you might be happier with the ereg versions.