如何在 PHP 5.6 中使用带有文件扩展名的 preg_match
How to use preg_match with file extension in PHP 5.6
我正在寻找名称类似于此字符串的文件:.047b2edb.ico
我不确定如何向我的 preg_match
函数添加“ico
”扩展。
[.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)
我正在寻找名称类似于此字符串的文件:.047b2edb.ico
我不确定如何向我的 preg_match
函数添加“ico
”扩展。
[.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)