Search code examples
arrayspreg-grep

PHP preg_grep multi files


how can I exclude more files like branding.php? For example branding.php, about.php, contact.php.

$pages = preg_grep('/branding\.php/', glob("*.php"), PREG_GREP_INVERT);

Thx.


Solution

  • Use alternative in the regex:

    $pages = preg_grep('/\b(?:branding|about|contact)\.php/', glob("*.php"), PREG_GREP_INVERT);
    

    Where

    • \b   : word boundary to avoid matching abranding or abc123about ...
    • (?:branding|about|contact) : non capture group that match branding OR about OR contact (you may add more files)