PHP preg_grep 多文件

Posted

技术标签:

【中文标题】PHP preg_grep 多文件【英文标题】:PHP preg_grep multi files 【发布时间】:2018-09-14 06:23:37 【问题描述】:

如何排除更多像branding.php 这样的文件? 例如branding.php、about.php、contact.php。

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

谢谢。

【问题讨论】:

【参考方案1】:

在正则表达式中使用替代:

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

在哪里

\b   :字边界以避免匹配 abrandingabc123about ... (?:branding|about|contact) :匹配 branding OR about OR contact 的非捕获组(您可以添加更多文件)

【讨论】:

这行不通,伙计。只有我的第一个代码示例运行良好,但这不是解决方案。

以上是关于PHP preg_grep 多文件的主要内容,如果未能解决你的问题,请参考以下文章