RegEx pattern in glob function

心不动则不痛 提交于 2019-12-03 13:32:41
Sam

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 (\.).

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