Home > Article > Backend Development > (PHP) Regular expression-usage of preg_match and preg_match_all()
<?php /** * 正则表达式练习 * User: Ollydebug * Date: 2015/11/13 * Time: 13:28 */ /* * preg_match()第三个参数可选,第三个参数是引用传递,它在匹配subject的时候,只会匹配一次 * preg_match_all()第三个参数必填,第三个参数也是引用传递,它在匹配subject的时候,会把所有满足条件的结果都匹配出来 */ $pattern = '/[0-9]/'; $subject = 'weuyr3ui76as83s0ck9'; $m1 = $m2 = array(); $t1 = preg_match($pattern,$subject,$m1); $t2 = preg_match_all($pattern,$subject,$m2); show($m1); echo '<hr/>'; show($m2); echo '<hr/>'; show($t1.'||'.$t2); function show($var){ if(empty($var)){ echo 'null'; }elseif(is_array($var)||is_object($var)){ // array,object echo '<pre class="brush:php;toolbar:false">'; print_r($var); echo ''; }else{ //string,int,float echo $var; } } ?>
The above is the usage of (PHP) regular expressions-preg_match and preg_match_all(). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!