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

Regular expressions in PHP match, match_all

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:00:07
Original
1373 people have browsed it

  • 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>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
How to match the environment
From 1970-01-01 08:00:00
0
0
0
javascript - About match()
From 1970-01-01 08:00:00
0
0
0
Regular expression to match words
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template