PHP Regular Expressions Summary of common functions (code example)

Source: Internet
Author: User

This article brings the content is about PHP regular expression Common functions of the summary (code example), there is a certain reference value, the need for friends can refer to, I hope to help you.

Regular expression functions commonly used in PHP
* Preg_match ()
* Preg_match_all ()
* Preg_replace ()
* Preg_filter ()
* PREG_GREP ()
* Preg_split ()
* Preg_quote ()

The following comparison explains:

$pattern usually regular expressions $subject usually represent the target processing data
Define a method for easy viewing of data types:

Function Show ($val = null) {    if (empty ($val)) {            //print empty        echo ' Mull ';    } ElseIf (Is_array ($val) | | is_object ($val)) {            //print array or object        echo ' <pre> ';        Print_r ($val);                Echo ' </pre> ';    } else{            //print string int float        echo $val;    }}
    1. Preg_match () and Preg_match_all ()

  /* Method Simple Description */  Preg_match ($pattern, $subject, [array & $matches])    //Match once  third parameter optional  match result place matches array  Preg_match_all ($pattern, $subject, array &matches)   //Match multiple  three-parameter required      match results place the matches array  $pattern = '/[0-9]/';  $subject = ' b5fg4sgf3sd2f1g ';  $m 1 =preg_match ($pattern, $subject, $arr 1), Show ($m 1), Show ($arr 1),/* 1Array (    [0] = 5) */$m 2 =preg_match_all ($ Pattern, $subject, $arr 2), Show ($m 2), Show ($arr 2),/* 5Array (    [0] = = Array        (            [0] = 5            [1] = = 4            [2] = 3            [3] = 2            [4] = 1        )) */

2.preg_replace () and Preg_filter ()

/* Method Simple Description *//* * * preg_replace and Preg_filter * preg_replace ($pattern, $replacement, $subject) *//Regular replacement function is replaced by an array of objects is returned all// The regular replacement function is replaced by an array of objects that are returned only by the substituted element * Lenovo: Str_repalce () * Echo Str_replace ("World", "Shanghai", "Hello world!"); *//Get Hello shanghai!//replace string in first Test string conclusion: here Preg_replace () and Preg_filter () no difference $pattern = '/[0-9]/'; $subject = ' b65fg4sgf3sd2f1g '; $replacement = ' _ '; $str 1 = preg_replace ($pattern, $replacement, $subject); $str 2 = Preg_filter ($ Pattern, $replacement, $subject); Echo ' 

3. Preg_grep ($pattern, array $subject)//Here $subject can only be a one-dimensional array

/* Function Description */preg_grep ($pattern, array $input)//can only match one-dimensional array return matching successful array elements can be understood Preg_grep () for the castrated version of Preg_filter () only did the match do not replace $pattern = '/[0-9]/'; $subject =array (' B ', ' fg3 ', ' 456 ', ' SGF ', ' 789 ', ' FG '); $arr = Preg_grep ($pattern, $subject); show ($arr);/* Array (    [1] = Fg3    [2] = 456    [4] = 789) */

4. Preg_split ($pattern, $subject)//A match successfully cuts the string for the cutting point array
can understand Preg_split () upgraded version of Explode ()

$pattern = '/[0-9]/'; $subject = ' BFG3456SGF789FG '; $arr = Preg_split ($pattern, $subject); show ($arr);/*array (    [0] = > BFG    [1] =    [2] = [3] = [    4] = SGF    [5] =    [6] =    [7] = FG) */

5. Preg_quote ($STR) regular operator translation

/* * Preg_quote ($STR) The regular operator translates *  . \ + *? [ ^ ] $ () {} = ! < > | :-* * */echo ' 

Simple summary of the continuity:
Preg_ Opening
Except for the Preg_quote function, the first argument is a regular expression
Preg_match form validation, etc.
Preg_replace illegal word filter, etc.

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.