What is missing here for the result, but it’s not clear what. It produces an error in the string result [] = $ arr [$ i];

function search_dir($dir, $word){ $arr=(scandir($dir)); for ($i = 0; $i < count($arr); $i++) { $pos=strpos($arr[$i],$word); if($pos!==false){ $result[]=$arr[$i]; } }return $result; } print_r(search_dir(__DIR__,'name')); 
  • But what error do you basically say? - Alexey Shimansky
  • undefined variable $ result. Tried to initialize it in the function, but nothing comes out. - Beginner
  • how and where did you try to initialize it, show it? - Alexey Shimansky
  • Before the line $ arr = (scandir ($ dir)); wrote $ result = array (""); - Beginner
  • one
    I now can not understand what, and what prevents to use glob? $result = glob($dir.'/*'.$word.'*'); - Stanislav

1 answer 1

Add $result = array();

 function search_dir($dir, $word) { $result = array(); $arr = (scandir($dir)); for ($i = 0; $i < count($arr); $i++) { if (('.' != $arr[$i]) && ('..' != $arr[$i])) { $filename = $dir . $arr[$i]; // Возможно, $dir . '/' . $arr[$i] if(is_file($filename)){ $x = file_get_contents($dir . $arr[$i]); $pos = strpos($x, $word); if ($pos !== false) { $result[] = $arr[$i]; } } } } return $result; } print_r(search_dir(__DIR__, 'name')); 
  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash