Anonymous Anonymous - 1 year ago 159
PHP Question

How can I see if a string matches a PHP array of regex values?

I am using my URI's to determine if the page footer should be included or not. I have an array of regex patterns and I want to see if the current uri matches any of the regex patterns in my array. How can I do this?

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

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

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

}

Answer

You have to loop through the entire array and check each and every one of the array elements with 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;
    }
}
?>