<code><span>//preg_match,preg_match_all</span><span>$pattern</span> = <span>'/[0-9]/'</span>; <span>$subject</span> = <span>'y1jp78yn16ww55'</span>; <span>//定义两个空数组</span><span>$m1</span> = <span>$m2</span> = <span>array</span>(); <span>$t1</span> = preg_match(<span>$pattern</span>,<span>$subject</span>,<span>$m1</span>);<span>//$m1输出的是一维数组;把返回值赋给$t1</span><span>$t2</span> = preg_match_all(<span>$pattern</span>,<span>$subject</span>,<span>$m2</span>);<span>//$m2输出的是二维数组;把返回值赋给$t2</span> show(<span>$m1</span>);<span>//使用show函数进行调试输出</span><span>echo</span><span>'<hr />'</span>; show(<span>$m2</span>); <span>echo</span><span>'<hr />'</span>; show(<span>$t1</span>.<span>'||'</span>.<span>$t2</span>);<span>//匹配到的次数</span></code>
preg_match matches at most once , the return result is only 0 or the first matched element, in array form.
preg_match_all matches all. The return result may be 0 or all matched elements, in the form of a multi-dimensional array.
The above has introduced the PHP chapter of match and match_all on regular expressions, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.