Dylan Marshall
Dylan Marshall

Reputation: 3

Case Sensitivity

i found a php search script online that works great for me. im having a problem with case sensitivity though. this search is linked to an html form input and will only return results that match the case searched. is there a way around this?

<?php
   //////////////////////
  // © Nadav Ami 2009 //
 //     Version 1.2  //
 //     Edited By  //
 //    Geoff Bolton //
//////////////////////


function scandir_r($dir){
$files = array_diff(scandir($dir), array(".", ".."));
$arr = array();
    foreach($files as $file){
    $arr[] = $dir.DIRECTORY_SEPARATOR.$file;
        if(is_dir($dir.DIRECTORY_SEPARATOR.$file)){
        $arr = array_merge($arr, scandir_r($dir.DIRECTORY_SEPARATOR.$file));
        }
    }
return($arr);
}

$dirname = "./";
$findme = "/".preg_quote($_POST["search"], "/")."/";
$files = preg_grep($findme, scandir_r($dirname));
if(sizeof($files)){
    foreach($files as $file){
    $_file = $dirname.DIRECTORY_SEPARATOR.$file;
    echo "<a href=\"$_file\">$file</a><br/>";
    }
}
else{
echo "Nothing was found.";
}
?>

Upvotes: 0

Views: 62

Answers (1)

skroczek
skroczek

Reputation: 2309

You can use the case-insensitive search. It's demonstrated in the first example of preg_match (http://php.net/manual/en/function.preg-match.php) and should also work for preg_grep. so this should do the trick:

// [...]
$findme = "/".preg_quote($_POST["search"], "/")."/i";
// [...]

Upvotes: 1

Related Questions