[TOC] ## 是否符合正则 ``` if (preg_match("/php/i", "PHP is the web scripting language of choice.", $matches)) { print "A match was found:" . $matches[0]; } else { print "A match was not found."; } ``` > 不匹配返回0.匹配返回1 ## 匹配并返回多个值 ``` preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches); /** * Array( [0] => foobarbaz [1] => foo [2] => bar [3] => baz ) */ print_r($matches); ``` ## 给匹配的值命名 ``` $str = 'foobar: 2008'; preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches); /** Array( [0] => foobar: 2008 [name] => foobar [1] => foobar [digit] => 2008 [2] => 2008 ) */ print_r($matches); ``` ## 全局匹配 preg_match_all ``` preg_match_all("/(5\d5)-/x", "Call 555-1212 or 1-800-545-1212", $phones); // [['555-','555-'],['555',['545']]] print_r($phones); ```