PHP regular expression preg_match detailed rules _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Introduction to the use rules of PHP regular expression preg_match. Use of PHP regular expression preg_match: using preg_match (), we can complete string rule matching. If a match is found, the preg_match () function returns 1; otherwise, 0. Use of PHP regular expression preg_match:

With preg_match (), we can complete string rule matching. If a match is found, the preg_match () function returns 1; otherwise, 0. Another optional third parameter allows you to store the matched part in an array. This function can be very useful when verifying data.

 
 
  1. $ String = "football ";
  2. If (preg_match ('/foo/', $ string ))
  3. {
  4. // The matching is correct.
  5. }

The above example will be matched successfully because the word football contains foo. Now let's try a more complex one, such as verifying an Email address.

 
 
  1. $ String = "first.last@domain.uno.dos ";
  2. If (preg_match ('/^ [^ 0-9] [a-zA-Z0-9 _] +
    ([.] [A-zA-Z0-9 _] +) * [@] [a-zA-Z0-9 _] + ([.]
    [A-zA-Z0-9 _] +) * [.] [a-zA-Z] {2, 4} $/', $ string ))
  3. {
  4. // Verify the Email address
  5. }

This example verifies that the Email address is in the correct format.

PHP regular expression preg_match rules:

Through the demonstration of the above example, we will understand the various rules represented by this regular expression.

PCRE, as its name implies, has the same syntax as the regular expression in Perl, so each regular expression must have a pair of delimiters. We generally use the separator.

The ^ at the beginning and $ at the end let PHP check from the beginning of the string to the end. If there is no $, the program will still match to the end of the Email.

◆ [And] are used to limit the license input type. For example, a-z allows all lowercase letters, A-Z allows all uppercase letters, 0-9 all numbers, and so on, and more other types.

◆ {And} are used to limit the expected number of characters. For example, {2, 4} indicates that each section of a string can contain 2-4 characters, such as .com.cn or. info. Here, "." is not a single character, because the allowed input type defined earlier than {2, 4} only contains uppercase and lowercase letters, so the segment only matches uppercase and lowercase letters.

◆ (And) is used to merge sections and define the characters that must exist in strings. (A | B | c) can match a, B, or c.

◆ (.) Will match all characters, while [.] will only match "." itself.

To use some symbols, you must add one in front. These characters are: () []. *? + ^ | $

This section describes the PHP regular expression preg_match. it is helpful for you to understand and master PHP preg_match regular expressions.


Using preg_match (), we can complete string rule matching. If a match is found, the preg_match () function returns 1; otherwise, 0 is returned ....

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.