test preg snippet

No Comments

just a little piece of code to test php preg_match(). I used it to test regexp on urls with Zend_Cache_Frontend_Page

$regexp='\?page';//just a regexp (without delimiter)
checkPreg($regexp);
checkPreg($regexp,$_SERVER['REQUEST_URI']);

function checkPreg($preg,$url='http://domain.com/index.php?page=shop/cart2'){
    $res=preg_match(sprintf('|%s|',$preg),$url);
    echo $preg ." match with $url ? ";
    if(false===$res){
        echo 'ERROR';
    } elseif(0 === $res) {
        echo 'NON';
    } else {
        echo 'YES. Num = '.$res;
    }
    echo "<br />\n";
}
Share

Leave a Reply