Created
September 3, 2015 20:57
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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