<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 最多匹配一次,返回结果只为0或第一个匹配到的元素,数组形式。
preg_match_all 全部匹配。返回结果可能为0或所有匹配到的元素,多维数组形式。
以上就介绍了正则表达式之PHP篇match,match_all,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。