The regular function of PHP

Source: Internet
Author: User
Tags preg

PHP, like JavaScript, regular matching content is usually used//wrapped up, you can use other non-letters and numbers to play the same role, can not use \ \ \
$preg = '/demo/';

Preg_match


   
    $str = "abcdefgabcdbc";    $pattern = '/[da]bc/';    echo'preg_match:'.preg_match($pattern, $str).'
'; echo'preg_match_all:'.preg_match_all($pattern, $str,$matches).'
'; //preg_match_all匹配所有,并将其匹配项带入第三个参数 $matches var_dump($matches); ?>//preg_match:1preg_match_all:3array(1) { [0]=> array(3) { [0]=> string(3) "abc" [1]=> string(3) "abc" [2]=> string(3) "dbc" } }

Preg_replace


    
     $str = '2014-11-25';    //使用()包括起来代表一个单元,可以与${n}对应使用,匹配的第几个内容会带入对应的${n}$preg = '/(-)/';    $temp = 'Year';    echo preg_replace($preg,$temp.'${1}',$str);    echo'
'; $preg = array( '/(\d+)/' ); $temp = array( 'temp[${1}]' ); echo preg_replace($preg,$temp,$str);?>//2014Year-11Year-25temp[2014]-temp[11]-temp[25]

Preg_split


    
     $str = '1+2-3/4*5';$pattern = '#[+-/*]#';$res = preg_split($pattern,$str);print_r($res);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP regular function, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.