note 57523 modified in function.strpos by danbrown

From: Date: Sun, 17 May 2009 16:38:38 +0000
Subject: note 57523 modified in function.strpos by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154870@lists.php.net to get a copy of this message
this function takes a space-deliminted string as a list of potential needles and runs it against another string as a haystack. the number of positive matches of needles within the haystack is returned as a rounded percentile. <?php function keyMatch($needles,$haystack) { $nArray=split(" ",$needles); $found=0; $best=count($nArray); for($i=0;$i<count($nArray);$i++) { $pzn=strpos(strtoupper($haystack),strtoupper($nArray[$i])); if ($pzn>-1) { $found++; } } $pct=($found*100)/$best; return round($pct); } $test = keyMatch("the quick wolf","the quick brown fox jumps over the lazy dog"); echo($test); ?> RESULT: 67 (because "the" and "quick" were found but "wolf" was not) --was-- this function takes a space-deliminted string as a list of potential needles and runs it against another string as a haystack. the number of positive matches of needles within the haystack is returned as a rounded percentile. function keyMatch($needles,$haystack) { $nArray=split(" ",$needles); $found=0; $best=count($nArray); for($i=0;$i<count($nArray);$i++) { $pzn=strpos(strtoupper($haystack),strtoupper($nArray[$i])); if ($pzn>-1) { $found++; } } $pct=($found*100)/$best; return round($pct); } $test = keyMatch("the quick wolf","the quick brown fox jumps over the lazy dog"); echo($test); RESULT: 67 (because "the" and "quick" were found but "wolf" was not) http://php.net/manual/en/function.strpos.php

« previous php.notes (#154870) next »