[PHP] Re: Search code finds ANYTHING

2001-08-15 Thread Hugh Bothwell


Raphael Pirker [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...

 function search_the_array($array) {
   global $search_tmp;
   $result = array_search($search_tmp, $array, TRUE);
reset($array);
while (!($result === FALSE)  (list($k, $v) =
each($array)))
 {
// Or maybe you wanted ereg($search_tmp, $v) here...
$result = ereg($v, $search_tmp) ? $k : FALSE;
}
 return ' '.$result; // extra space needed before the key
 }

This is a bit screwy...

First, array_search returns an array of keys where the values are
exact matches... while you want to match a substring (I think);

Then, it searches through the array again using regular expressions,
and each time it finds something, it over-writes all previous values...
and if there were any matches to begin with, it WILL find them again.

And why is the search-string passed as a global while the
array is passed by value?!  This makes no sense to me.

Finally, result is passed back as a string after being munged
so it can't be used as a key - and if nothing was found, it returns
a string consisting of a single space.


Try one of these instead:

// util function: case-sensitive search for substring,
//  returns true if found, else false
function findSameCase($haystack, $needle) {
return (strpos($haystack, $needle) !== false);
}

// util function: case-insensitive search for substring,
//  returns true if found, else false
function findAnyCase($haystack, $needle) {
return (strpos(strtolower($haystack), strtolower($needle)) !==
false);
}

// return the key of the first array member containing the search string,
//   or false if there are no matches.
function findFirst($arr, $string, $caseSensitive=false) {

if ($caseSensitive)
$find = findSameCase;
else
$find = findAnyCase;

foreach($arr as $key = $val)
if ($find($val, $string))
return $key;

return false;
}

// return an array of all keys to members containing the search
//   string (if there are no matches, the array is empty).
function findAll($arr, $string, $caseSensitive=false) {

if ($caseSensitive)
$find = findSameCase;
else
$find = findAnyCase;

$result = array();

foreach($arr as $key = $val)
if ($find($val, $string))
$result[] = $key;

return $result;
}




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Re: Search code finds ANYTHING

2001-08-15 Thread Raphael Pirker

Hi Hugh,

 First, array_search returns an array of keys where the values are
 exact matches... while you want to match a substring (I think);
Yes, but exact matches are better :))

 Then, it searches through the array again using regular expressions,
 and each time it finds something, it over-writes all previous values...
 and if there were any matches to begin with, it WILL find them again.
Problem is not the overwriting part.. problem is it doesn't really search,
it finds something instantly, whether it's inside the string or not

 And why is the search-string passed as a global while the
 array is passed by value?!  This makes no sense to me.
I'm searching a number of arrays (3 to be exact) for only one search
string... $array is different all the time while $search_tmp is the same

But thanks a lot for the scripts, I will be trying them out immediately!

Regards,

Raphael



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]