You can either do it in a "Positive" way, by making a regular expression which matches characters you want to allow, and checking if the strinc containg only those:
if (preg_match('/$[a-z0-9_\-\*\~]+^/i', $str)) { ...
Here I use $ to mean "beginning of string" and ^ to mean "end", the + means "one or more times" and the square brackets contain a character class consisting only of allowed chars. Some of the allowed chars need to be escaped.
The /i is a modifier which means "case insensitive"
Or negative, make a preg which matches just one character not in the set:
if (! preg_match('/[^a-z0-9_\-\*\~]/i')) {
// ok
This is basically the opposite - I'm looking for just one character, the ^ in the character class [] means any character NOT in that set.
Neither of these is tested, but you get the gist.
Mark