A summary of the matching and data validation of the regular expressions in PHP

Source: Internet
Author: User

Regular expressions can match complex string forms, much more than string-processing functions, but perform less efficiently, but can achieve very complex matches, as summarized below

1, simple string matching, to determine whether the specified string is in another string, and the string lookup function almost

 1   $p  = '/Apple/' 

It can be seen that the Preg_match () function is used to match the regular expression, and the argument is the expression (matching rule), the original string, which is the simplest regular expression.

2. Ignore Case matching

1 $p = '/bbc/i '; 2 $str = "BBC is a British television station"; 3 if (preg_match($p$str)) {4     Echo ' Match success! '; 5 }

You can see that this is also a successful match, with the expression followed by I for ignoring case matching

3. Common regular matches the data in the string (phone number, name, mailbox, HTML data)

1 //Match Phone number2 $p= '/\d+\-\d+/';3 $str= "My phone is 010-12345678";4 Preg_match($p,$str,$match);5 Echo $match[0];6 Echo"<br/>";7 //Match name8 $p= '/[\w\s+]:([\w\s]+)/';9 $str= "Name:zengzhiying";Ten Preg_match($p,$str,$match); One Echo $match[1]; A Echo"<br/>"; - //Match Email address - $subject= "my email is [email protected]"; the $pattern= '/\[email protected]\w+\.\w+$/'; - Preg_match($pattern,$subject,$mathes); - Echo $mathes[0]; - Echo"<br/>"; + //Match HTML Data - $str= "<ul> + <li>item 1</li> A <li>item 2</li> at</ul> "; - $p= '/<li> (. *?) <\/li>/i '; - Preg_match_all($p,$str,$matches); - Print_r($matches[1]);

Here the third parameter of the Preg_match () function is the result of the match obtained, is an array, if you do not know $match which element is to find, you can first Print_r try, and then determine the final result

4. Capturing specific data in a crawl page

Sometimes we need to crawl some specific data of other sites, first we crawl to the entire page of HTML content, and then put them into a string, and then we want to get the value of one of the elements, this time we need to match the regular expression, see a case, When we log on to the site generally in addition to submit user name, password, verification code and other data generally have hidden fields, value inside a very long string, with different clients will change, this is to prevent some illegal operation, if only so we can crawl this data, Thus in the server side of the simulation login, the same crawl to the user login content, PHP can be sent with curl request to implement, now just grab the Value property values, the code is as follows:

1 $data= "<input type=\" hidden\ "name=\" __viewstate\ "value=\" shssksswhjfjnvjsuehdj28u3uej-9wj\ "/>" ; 2 $pattern = '/<input type= ' hidden "name=" __viewstate "value=" (. *?) "\/>/is"; 3 Preg_match_all ($pattern$data$matches); 4 Echo $matches [1] [0];

So we're outputting specific data, that's SHSSKSSWHJFJNVJSUEHDJ28U3UEJ-9WJ.

5. Verify that the data cannot be empty

Sometimes user input information we get through GET or post, first to determine that a variable can not be null, this PHP built-in function empty can be, the following is a simple function

1 functionNoempty ($user){2     if(Empty($user)){3         $ret=0;4}Else{5         $ret=1;6     }7     return $ret;8}

The return value we can use other values, in short, convenient for us to use, can be used as a judge can also output information

6. Verify the mailbox format

1 functionEmailver ($email){2      $pattern= '/^[\w\.] [Email protected]\w+\.\w+$/i ';3      if(!Preg_match($pattern,$email)) {4          $ret=0;5}Else {6          $ret=1;7      }8      return $ret;9}

7, verify the mobile phone number

1 functionMobilever ($mobile) {2     $pattern= '/^1\d{10}$/i ';3     if(!Preg_match($pattern,$mobile) {4         $ret=0;5}Else {6         $ret=1;7     }8     return $ret;9}

Follow up and continue to supplement

A summary of the matching and data validation of the regular expressions in PHP

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.