Search code examples
phppreg-match

use PHP preg_match on directory name - Unknown modifier


can anyone advise the correct way to use preg_match on directory names.

Ive used glob - to get a small array of files and I am wanting to use preg_match on the resulting foreach INCLUDING the directory..

however I am getting this error - preg_match(): Unknown modifier 'h' in ....

H of course is the 1st letter of my directory after the initial slash - /

EDIT - Code ($path includes the full path name)

$temp="$path/$name-$type-$number\n";
// $temp=preg_replace("/\//","\/",$temp);
$files=glob("$path/$name*");
foreach($files as $ab => $ac)
{
  $outlist.="'$ab' '$ac'\n";
  $outlist.="TEMP '$temp'\n";
  if(preg_match("/$temp/i",$ac))
  {
    $outlist.="GOT '$ac'\n";
  }
}

Solution

  • so don't use the / as the delimiter for your regex. Or escape the slashes in your input.

    if(preg_match("~$temp~i",$ac)){$outlist.="GOT '$ac'\n";}

    assuming the ~ is never part of $temp

    or by escaping:

    $temp2 = preg_quote($temp, '/');
    if(preg_match("/$temp2/i",$ac)){$outlist.="GOT '$ac'\n";} `