我正在寻找一个文件名类似于以下字符串的文件:.047b2edb.ico
我不确定如何将"ico
"扩展添加到我的preg_match
函数中。
[.a-zA-Z0-9]
任何建议将不胜感激
这是我的全部代码。使用此代码,我找不到名为.62045303的文件.ico问题出在哪里?
<?php
$filepath = recursiveScan('/public_html/');
function recursiveScan($dir) {
$tree = glob(rtrim($dir, '/') . '/*');
if (is_array($tree)) {
foreach($tree as $file) {
if (is_dir($file)) {
//echo $file . '<br/>';
recursiveScan($file);
} elseif (is_file($file)) {
if (preg_match_all("(/[.a-zA-Z0-9]+.ico/)", $file )) {
//echo $file . '<br/>';
unlink($file);
}
}
}
}
}
?>
[.a-zA-Z0-9]+.ico
会做到的。
解释:
[.a-zA-Z0-9] match a character which is a dot, a-z, A-Z or 0-9
+ match one or more of these characters
.ico match literally dot followed by "ico".
the backslash is needed to escape the dot as it is a metacharacter
例:
$string = 'the filenames are .asdf.ico and fdsa.ico';
preg_match_all('/[.a-zA-Z0-9]+.ico/', $string, $matches);
print_r($matches);
输出:
Array
(
[0] => Array
(
[0] => .asdf.ico
[1] => fdsa.ico
)
)
根据您要匹配的内容,这可能对您有好处
([.a-zA-Z0-9]+)(.ico)