PHP learning notes-Example code about regular expressions in PHP textbooks (Abstract)

Source: Internet
Author: User
Tags ereg preg
<? PHP  /*  POSIX extended regular expression Functions  */          /*  ---------------------------------------------------  */          $ Ereg = '^ [$] [[: Alpha:] _] [[: alnum:] *' ;  Ereg ( $ Ereg , '$ _ Name ', $ Register ); //  Match and store arrays // eregi () case-insensitive         Var_dump ( $ Register ); //  Display Array Structure                  /*  ---------------------------------------------------  */          $ Ereg = 'TT' ;  $ Str = 'Hello, TM' ;  $ Rep_str = Eregi_replace ( $ Ereg , 'TT ',$ Str ); //  If the matching is successful, use TM to replace ereg_replace () in case sensitivity.          Echo   $ Rep_str  ;  /*  ---------------------------------------------------  */          $ Ereg = 'Is' ;  $ Str = 'This is a register book .' ;  $ Arr_str =Spliti ( $ Ereg , $ Str ); //  Split the string. Split () matching by regular expression is case sensitive          Var_dump ( $ Arr_str  );  /*  PCRE is compatible with regular expression functions-PCRE is slightly better than POSIX in terms of efficiency or syntax  */          /*  ---------------------------------------------------  */         $ Preg = '/\ D {3, 4 }-? \ D {7, 8 }/' ;  $ Arr = Array ('20140901', '2014-043212345678 ', '20140901 '); //  Contains element array          $ Preg_arr = Preg_grep ( $ Preg , $ Arr  );  Var_dump ( $ Preg_arr  ); /*  ---------------------------------------------------  */          $ Str = 'This is an example! ' ;  $ Preg = '/\ B \ W {2} \ B /' ;  $ Num1 = Preg_match ( $ Preg , $ Str , $ Str1 ); // Match and return the number of successful results. After the successful match is put into the array, it stops.          Echo   $ Num1 . "<Br/>" ;  Var_dump ( $ Str1  );  Echo "<Br/>" ;  //  Returns the number of successful matches. After successful matches are put into the array, the system does not stop until all matching characters are completed.          $ Num2 = Preg_match_all ( $ Preg ,$ Str , $ Str2  );  Echo   $ Num2 . "<Br/>" ;  Var_dump ( $ Str2  );  /*  ---------------------------------------------------  */          $ Str = '! , $, ^, *, +,., [,], \,/, B, <,>' ; $ Str2 = 'B' ;  //  Automatically escape special regular expressions. If a parameter exists, the characters containing the parameter are also escaped.          $ Match_one = Preg_quote ( $ Str , $ Str2  );  Echo   $ Match_one  ;  /*  --------------------------------------------------- */          $ String = '[B] Bold [/B]' ;  //  Reverse references are used for matching and replacement.          $ B _rst = Preg_replace ('/\ [B \] (. *) \ [\/B \]/I', '<B> $1 </B> ', $ String  );  Echo   $ B _rst  ;  /*  --------------------------------------------------- */          Function C_back ( $ Str  ){  //  Reverse reference              $ Str = "<Font color = $ Str [1]> $ Str [2] </font>" ;  Return   $ Str  ;}  $ String = '[Color = Blue] font blue [/color]';  //  The character to be replaced in the preg_replace_callback function. It is only the same as the preg_replace function.          Echo   Preg_replace_callback ('/\ [Color = (. *) \] (. *) \ [\/color \]/U', "c_back ", $ String  );  /*  ---------------------------------------------------  */          //  No in this function book. I will give you my own examples!          $ Preg = "/\ S + /";//  Here you can also change to "/\ s {1 ,}/";          $ Str = "I'm Chinese! I can eat" ;  $ Arr = Preg_split ( $ Preg , $ Str ); // Search and split. -- There is a written mistake here. The friends I have read can't help but have corrected it now.          Var_dump ( $ Arr  ); ?>

 

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.