如何在PHP5.6中使用带文件扩展名的preg_match
我正在寻找一个名称类似以下字符串的文件:如何在PHP5.6中使用带文件扩展名的preg_match,php,preg-match,ico,Php,Preg Match,Ico,我正在寻找一个名称类似以下字符串的文件:.047b2edb.ico 我不知道如何将“ico”扩展添加到我的preg\u match函数中 [.a-zA-Z0-9] 如有任何建议,将不胜感激 这是我的全部密码。使用此代码,我找不到名为.62045303.ico的文件。问题出在哪里 <?php $filepath = recursiveScan('/public_html/'); function recursiveScan($dir) { $tree = glob(rtrim($
.047b2edb.ico
我不知道如何将“ico
”扩展添加到我的preg\u 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] 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] 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)
根据您想要匹配的内容,这可能对您有好处
([.a-zA-Z0-9]+)(\.ico)
警告:preg\u match():未知修饰符“+如何修复此问题?@AdamKowalski:您没有在正则表达式周围放置分隔符!什么分隔符?@AdamKowalski:Regex分隔符(即/[.a-zA-Z0-9]+\.ico/
)不起作用。我的代码:if(preg_-match(“/[.a-zA-Z0-9]+\.ico/”,$file)){警告:preg_-match():未知修饰符“+”如何解决此问题?@AdamKowalski:您没有在正则表达式周围放置分隔符!什么分隔符?@AdamKowalski:regex分隔符(即,/[.a-zA-Z0-9]+\.ico/
)不起作用。我的代码:if(preg_-match(“/[.a-zA-Z0-9/”,$ico/”){字符类内不需要转义点。字符类内不需要转义点。