Как использовать preg_match с расширением файла в PHP 5.6 - PullRequest
0 голосов
/ 02 мая 2018

Я ищу файл с именем, похожим на эту строку: .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);
               }

            }
        }
    }
}
?>

Ответы [ 2 ]

0 голосов
/ 02 мая 2018

В зависимости от того, что вы хотите сопоставить, это может быть полезно для вас

([.a-zA-Z0-9]+)(\.ico)

0 голосов
/ 02 мая 2018
[.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
        )

)
...