Home >Backend Development >PHP Tutorial >Regular expressions in PHP match, match_all

Regular expressions in PHP match, match_all

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:00:071375browse

  • preg_match($pattern,$subject,[array&match]) preg_match_all($pattern,$subject,array&matches)
    preg match only matches once and returns a value of 0 or 1;
    preg match all matches multiple times, how many times it matches and returns the number of matches;
    $pattern regular expression;
    $subject the string or array to be matched;
<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.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn