Home  >  Article  >  Backend Development  >  (PHP) Regular expression-usage of preg_match and preg_match_all()

(PHP) Regular expression-usage of preg_match and preg_match_all()

黄舟
黄舟Original
2016-12-30 11:05:401403browse

<?php
/**
 * 正则表达式练习
 * User: Ollydebug
 * Date: 2015/11/13
 * Time: 13:28
 */

/*
 * preg_match()第三个参数可选,第三个参数是引用传递,它在匹配subject的时候,只会匹配一次
 * preg_match_all()第三个参数必填,第三个参数也是引用传递,它在匹配subject的时候,会把所有满足条件的结果都匹配出来
 */

$pattern = &#39;/[0-9]/&#39;;
$subject = &#39;weuyr3ui76as83s0ck9&#39;;
$m1 = $m2 = array();
$t1 = preg_match($pattern,$subject,$m1);
$t2 = preg_match_all($pattern,$subject,$m2);

show($m1);
echo &#39;<hr/>&#39;;
show($m2);
echo &#39;<hr/>&#39;;
show($t1.&#39;||&#39;.$t2);

function show($var){
    if(empty($var)){
        echo &#39;null&#39;;
    }elseif(is_array($var)||is_object($var)){
        // array,object
        echo &#39;<pre class="brush:php;toolbar:false">&#39;;
        print_r($var);
        echo &#39;
'; }else{ //string,int,float echo $var; } } ?>

The above is the usage of (PHP) regular expressions-preg_match and preg_match_all(). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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