Forum Moderators: coopster

Message Too Old, No Replies

Check if variable matchs part of an array

         

ntbgl

11:50 am on May 27, 2009 (gmt 0)

10+ Year Member



I would like to set a variable's value based on if one variable matches part of my array.

Right now I use this:

if(in_array($domain,$program)) $track=true;

With $program being an array looking like this:

$program=array("law.cornell.edu","gpoaccess.gov");

Right now $track is set true if $domain matches "law.cornell.edu" or "gpoaccess.gov", but I would like to set program to something like:

$program=array(".edu",".gov","google.co.uk","amazon.com");

That way, all .edu sites, all .gov sites, and only specified other sites make $track=true;

How can I do this? in_array seems close, but isn't quite right.

Thanks

coopster

1:16 pm on May 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That is one way to do it, yes. Set the array to contain just the TLD as you have shown there using ".edu", ".gov", etc. and then if the domain is an .edu or .gov, set the $domain for checking to be the same.

Your other option will be to loop through the $program array and use a regular expression to check each value as you iterate the array.