Page 1 of 1

Checking for characters in a string

Posted: Sun Mar 04, 2007 1:39 pm
by mjseaden
Hi,

I have a $var which contains a set of characters. I want to check that another string, $string, ONLY contains characters contained within $var.

Is there any PHP string function that does this?

Many thanks.

Posted: Sun Mar 04, 2007 2:57 pm
by Christopher
You will need to play with the logic, but maybe something like this:

Code: Select all

if (preg_match("[^$var]", $string) == 0) {
That is checking to see if a character not in the set of characters is not found ... hard to follow the double negative though.

Posted: Sun Mar 04, 2007 6:36 pm
by feyd
You should probably pass $var through preg_quote() as well.