Skip to content

Instantly share code, notes, and snippets.

@ababkov
Created September 3, 2015 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ababkov/6f3f78c6272ec94b3867 to your computer and use it in GitHub Desktop.
Save ababkov/6f3f78c6272ec94b3867 to your computer and use it in GitHub Desktop.
Expand /**/ in a php glob expression to simulate nested folder expressions (which otherwise aren't supported for some reason)
function expandExpression($path,$depth=3,$marker="/**/"){
$e_path = explode($marker,$path);
if( count($e_path) > 2 ){
throw new \InvalidArgumentException("Only one instance of '{$marker}' is supported per expression.");
} elseif( count($e_path) < 2 ){
return $e_path;
}
$base_path = $e_path[0];
$sub_path = $e_path[1];
$dir_globs = [];
for($i=0;$i<=$depth;$i++){
$filler = $i?array_fill(0,$i,"*/"):["/"];
$dir_globs[] = implode("",$filler);
}
return "{$base_path}/{".implode(",",$dir_globs)."}{$sub_path}";
}
$expression = "C:/Users/mypath/**/html_body.html";
$expanded = expandExpression($expression,3);
echo $expanded;
//C:/Users/mypath/{/,*/,*/*/,*/*/*/}html_body.html
$paths = glob($expanded,GLOB_BRACE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment