Php "glob" help

I have a directory with sitemaps that end in number.

I have an automatic Sitemap script generator that should count all Sitemaps in a folder using glob.

I'm currently stuck here.

I need to count all Sitemaps that have a number, so I don't count a number without any numbers.

For example, in my root I have sitemap.xml, then I also have sitemap1.xml, sitemap2.xml, sitemap3.xml, etc.

I need to use glob to return true only if the file name contains the number "sitemap1.xml".

Is it possible?

$nr_of_sitemaps = count(glob(Sitemaps which contains numbers in their filenames)); 

thank

+3
source share
1 answer

glob , <digit>.xml, , :

*[0-9].xml

, , PHP :

$count = count(glob('/path/to/files/*[0-9].xml'));

, - (moreso, glob ) , , preg_grep , .

$count = count(
    preg_grep(
        '#(?:^|/)sitemap\d{1,3}\.xml$#',
        glob('/path/to/files/sitemap*.xml')
    )
);

: http://cowburn.info/2010/04/30/glob-patterns/

+5

Source: https://habr.com/ru/post/1767068/


All Articles