Checking for characters in 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
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Checking for characters in a string

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You should probably pass $var through preg_quote() as well.
Post Reply