RegEx pattern in glob function

左心房为你撑大大i 提交于 2019-12-09 05:59:53

问题


I recive a filename in a function. I want to return all files similar to this file (by filename) from other directory. I wrote this:

    $thumbDir = $this->files_path.'thumbs/';
    $toglob = $thumbDir.pathinfo($name, PATHINFO_FILENAME ).'_[0-9]+\x[0-9]+_thb.'.pathinfo($name, PATHINFO_EXTENSION);
    foreach (glob($toglob) as $key => $value) {
        echo $value;
    }

But it doesn't work. I search files which their filename is:

oldFileName_[one or more digits]x[one or more digits]_thb.oldFileNameExtension

I will be very grateful if someone help me with this :)


回答1:


glob() is really a quasi-regex engine. From a comment on the docs, it allows a ? and a *:

glob uses two special symbols that act like sort of a blend between a meta-character and a quantifier. These two characters are the * and ?

The ? matches 1 of any character except a /

The * matches 0 or more of any character except a /

If it helps, think of the * as the pcre equivalent of .* and ? as the pcre equivalent of the dot (.)

This means you can't use your expression _[0-9]+\x[0-9]+_thb. in glob(). Instead, you can look in the whole directory and see if it matches with preg_match():

$glob = glob('/path/to/dir/*');
foreach($glob as $file) {
    if(preg_match('/_\d+x\d+_thb\./', $file)) {
        // Valid match
        echo $file;
    }
}

Realize that in glob(/path/to/dir/*);, the * does not match a / so this will not get any files in subdirectories. It will only loop through every file and directory in that path; if you want to go deeper, you will have to make a recursive function.

Note I cleaned your expression:

_\d+x\d+_thb\.

\d roughly equals [0-9] (it also includes Arabic digit characters, etc.), you do not need to escape x (so no \x), and you want to escape the period (\.).



来源:https://stackoverflow.com/questions/23566103/regex-pattern-in-glob-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!