如何查看字符串是否与正则表达式值的PHP数组匹配?

时间:2015-11-08 23:57:27

标签: php arrays regex

我正在使用我的URI来确定是否应该包含页脚。我有一组正则表达式模式,我想看看当前的uri是否匹配我的数组中的任何正则表达式模式。我怎么能这样做?

        $blocked = array(
            'blog/comments/add([0-9]+)'
        );

        if(/* uri does not match any regex pattern in the above array */){

            $this->load->view('overall_footer');

        }

1 个答案:

答案 0 :(得分:1)

你必须遍历整个数组并使用preg_match检查每个数组元素

<?php
 $blocked = array(
            'blog/comments/add([0-9]+)'
        );

foreach ($blocked as $current) {
    $result = null;
    $match = preg_match("#" . $current . "#", $_SERVER["REQUEST_URI"], $result);

    if($match == 1) {
        echo "found<br />";
        print_r($result);
        break;
    }
}
?>