PHP regular expression instance

Source: Internet
Author: User
Tags php regular expression
This condition statement is used to determine whether the start mark and the closed Mark are equal. if they are not equal, the mark is not closed. Finally, use in_array ($ start_tags [1] [$ I],

1. use a regular expression to check whether HTML is disabled
Code snippet
 

PHP code
  1. Function check_html ($ html ){
  2. Preg_match_all ("/<([a-zA-Z0-9] +) \ s * [^ \/>] *>/", $ html, $ start_tags );
  3. Preg_match_all ("/<\/([a-zA-Z0-9] +)>/", $ html, $ end_tags );
  4. If (count ($ start_tags [1])! = Count ($ end_tags [1]) return false;
  5. For ($ I = 0; $ I <count ($ start_tags [1]); $ I ++ ){
  6. If (! In_array ($ start_tags [1] [$ I], $ end_tags [1]) return false;
  7. }
  8. Return true;
  9. }

Explanation:
/<([A-zA-Z0-9] +) \ s * [^ \/>] *>/this pattern is used to match HTML tags (such:,
And so on,
And keep the label in $ start_tags.
(Such as head and div ). And/<\/([a-zA-Z0-9] +)>/this pattern is used to match closed HTML
Mark (for example:, Etc. And keep the closed tag name in $ end_tags. Then we use
Count ($ start_tags [1])! = Count ($ end_tags [1]) This condition statement is used to determine the starting tag and closed
Mark whether it is equal. if it is not equal, it indicates it is not closed. Finally, use in_array ($ start_tags [1] [$ I], $ end_tags [1]).
To determine whether the start mark is equal to the closed mark. So far, we have completed HTML matching!
2. match the email format
Code snippet
 

PHP code
  1. Function check_email ($ email ){
  2. If (preg_match ("/^ [\ w \ d! # $ % & '* +-\/=? ^ '{|} ~] + (\. [\ W \ d! # $ % & '* +-\/=? ^ '{|} ~] +) * @ ([A-z \ d] [-a-z \
  3. D] * [a-z \ d] \.) + [a-z] [-a-z \ d] * [a-z] $/", $ eamil) return true;
  4. Return false;
  5. }

Explanation:
Do not be
/^ [\ W \ d! # $ % & '* +-\/=? ^ '{|} ~] + (\. [\ W \ d! # $ % & '* +-\/=? ^ '{|} ~] +) * @ ([A-z \ d] [-a-z \ d] * [az \
D] \.) + [a-z] [-a-z \ d] * [a-z] $/
This mode is actually quite simple. Previous part
[\ W \ d! # $ % & '* +-\/=? ^ '{|} ~] + (\. [\ W \ d! # $ % & '* +-\/=? ^ '{|} ~] +) * Just match to conform to the RFC-2882 label
The quasi-email address can contain English letters, numbers, and symbols. if you are interested, you can query
RFC-2882 manual, and ([a-z \ d] [-a-z \ d] * [a-z \ d] \.) + is matching the HOST. Last [a-z] [-a-z \ d] * [a-z]
That is, it matches top-level domain names (such as. com and. org ).
3 non-greedy mode
When using regular expressions, you may find the following problems:
Code snippet
 

PHP code
  1. Preg_match ('/". *"/', 'Tony say: "hello", Jack say: "Hi" ', $ matches );
  2. Print_r ($ matches );


You are surprised to find that the matching content is "hello", Jack say: "Hi", not "hello" and
"Hi ". This is caused by greedy matching. In greedy match, regular expressions match the most words as much as possible.
So there is the first case, but what we want is the second case. what should we do?
We can use non-greedy match to change the pattern /".*? "/Then we can see what we want
The result is displayed. *? This is the non-greedy match mode. Another type is + ?.
? *? : The preceding characters can appear any number of times, *? The next character stops matching.
? +? : The preceding character can appear once or multiple times, but the +? The next character stops the match.
Configuration.
4. check whether a user password is secure.
Code snippet
 

PHP code
  1. Function is_good_pw ($ pw ){
  2. If (preg_match ('/(? =. * [0-9]) (? =. * [A-z]) (? =. * [A-Z]). {8}/', $ pw )){
  3. Return true;
  4. }
  5. Return false;
  6. }


Explanation:
In this example, we use /(? =. * [0-9]) (? =. * [A-z]) (? =. * [A-Z]). {8, 16}/pattern to match our
Password. In this mode, we use the forward-looking mode (? = ). (? =. * [0-9]) This is
There are numbers in the password, and (? =. * [A-z]) indicates that the password matches lowercase letters (? =. * [A-Z]) is to match the password
The code contains uppercase letters. The. {8, 16} password is a string of 8 to 16 characters. Then we
The password must contain numbers. a password consisting of uppercase and lowercase letters is a secure password ~
5. match all links in a website
Code snippet
 

PHP code
  1. Function get_links ($ link ){
  2. $ Html = file_get_contents ($ link );
  3. $ Html = str_replace ("\ n", "", $ html );
  4. $ Html = preg_replace ('/
  5. $ Html = preg_replace ('/<\/a>/', "\ n", $ html );
  6. Preg_match_all ('/.*? <\/A>/', $ html, $ matches );
  7. Return ($ matches );
  8. }


In this example, we want to use file_get_contents to obtain the content of a webpage. Then use
Str_replace ("\ n", "", $ html) removes all line breaks. Use preg_replace ('/<\/a>/', "\ n", $ html) to start a new line of all the... modes.
Use preg_match_all ('/.*? <\/A>/', $ html, $ matches) matches the link mode.
/.*? <\/A>/is the regular expression that matches the pattern. Why?
Do you want to add another line of link ?? Because in /.*? <\/A>/mode ,.*
It cannot match the line feed, so it cannot match if it is not in the same line !! So we need
Do it!


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.