Jump to content

struggling a bit with fnmatch for php 5.2.9 on windows


Greywacke
 Share

Recommended Posts

hi there,i need to check wether a certain string exists within an array of strings. there are two arrays that are checked respectively, with the following bit of php in the getsuppliers function.if $status = 0 it is an active supplier, if &status = 2 it is a prospecting supplier. the following excerpt is from inside an if statement.

if (!function_exists('fnmatch')) {	function fnmatch($pattern, $string) {		for ($op = 0, $npattern = '', $n = 0, $l = strlen($pattern); $n < $l; $n++) {			switch ($c = $pattern[$n]) {				case '\\':					$npattern .= '\\' . @$pattern[++$n];				break;				case '.': case '+': case '^': case '$': case '(': case ')': 				case '{': case '}': case '=': case '!': case '<': case '>': 				case '|':					$npattern .= '\\' . $c;				break;				case '?': case '*':					$npattern .= '.' . $c;				break;				case '[': case ']': default:					$npattern .= $c;					if ($c == '[') {						$op++;					} else if ($c == ']') {						if ($op == 0) return false;						$op--;					}				break;			}		}			if ($op != 0) return false;			return preg_match('/' . $npattern . '/i', $string);	}}function wc_in_array($needle, $haystack) {    # this function allows wildcards in the array to be searched    foreach ($haystack as $value) {        if (true === fnmatch($value, $needle)) {            return true;        }    }    return false;}

it doesn't seem the replacement fnmatch function accepts * characters as wildcards. could somebody please help!how could i fix this function? 0o i'm guessing to simplify it, to use stristr instead within the fnmatch replacement...i know it is not returning true if the value is found as i get duplicate suppliers in the array. the excerpt at the top is to check if the supplier has not been added yet.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

here is the replacement function and code used, still to check if it works:

function str_in_array($needle, $haystack) {	// this function allows case insensitive strings in the array to be searched	foreach ($haystack as $value) {		if (stristr($value, $needle)>-1) {			return true;		}	}	return false;}// if excerpt!str_in_array(";;;".$row1["bigint_SupplierID"].";;;",$GLOBALS[($status==2)?"mailaddp":"mailadds"])

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

it seems to be working perfectly!ISSUE RESOLVED :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...