1 <?php 2 // 显示汉字 3 header('content-type: text/html;charset = utf-8'); 4 5 /* preg_match($pattern, $subject, [array &$matches]) 第三个参数可选,也是引用参数 6 preg_match_all($pattern, $subject, array &$matches) 7 8 return 匹配到结果的次数; 9 */ 10 $pattern = '/[0-9]/'; 11 $subject = 'weuyr3ui76as83s0ck9'; 12 13 $m1 = $m2 = array(); 14 15 $t1 = preg_match($pattern, $subject, $m1); 16 $t2 = preg_match_all($pattern, $subject, $m2); 17 18 show($m1); 19 echo '<hr />'; 20 show($m2); 21 echo '<hr />'; 22 echo '$t1 & $t2 的返回值:'; 23 show($t1.'||'.$t2); 24 25 // 对输出做个判断,选择合适的输出方式 26 function show($var = null){ 27 if(empty($var)){ 28 echo 'null'; 29 }elseif(is_array($var) || is_object($var)){ 30 //array, object 31 echo '<pre>'; 32 print_r($var); 33 echo '</pre>'; 34 }else{ 35 //string, int, float... 36 echo $var; 37 } 38 }