How to use regular expressions in PHP (1)

Source: Internet
Author: User

What is a regular expression?

A few years ago, I made some interesting tests on the Web form input box. The user will enter the phone number in this form. The phone number is then printed in the user's advertisement as the user typed. According to requirements, the telephone number in the United States can be entered in several ways: (555) 555-5555, Or 555-555-5555, but not 555-5555.

You may wonder why we should not leave all non-numeric characters aside, but only ensure that the total number of remaining characters is 10? This method is feasible, but cannot prevent user input! 555? 333-3333.

From the perspective of a Web Developer, this situation brings an interesting challenge. I can write routines to check various formats, but I hope to find a solution. If the user subsequently accepts formats such as 555.555.5555, this solution can be flexible.

This is the applicable scenario for regular expressions (regex for short. I have cut and pasted them into the application before, but I have never found any incomprehensible Syntax problems. Regex looks like a mathematical expression. When you see an expression like 2x2 = 4, you usually think of "2 multiplied by 2 equals 4 ". The regular expression is very similar to the regular expression. After reading this article, when you see a regular expression ^ B $, you will tell yourself: "the beginning of a line is B, followed by the end of the line ". In addition, you will also realize how easy it is to use regular expressions in PHP.

Time to use regex

When there are rules to follow, you should use regex to complete the search and replacement operations, but do not have to have the exact characters to locate or replace. For example, in the phone number example mentioned above, the user defines a rule indicating the format of the entered phone number, but does not define the number contained in the phone number. This is also suitable for scenarios with a large number of user input. The abbreviation of the U.S. state name can be limited to two uppercase letters from A to Z. Here, you can also use regular expressions. You can simply restrict text or user input in a form to letters in the alphabet, regardless of case and length.

Time when regex is not suitable

Regular Expressions are powerful, but they also have some defects. One of these skills is required to have read and write expressions. If you decide to include a regular expression in your application, you should make a complete comment on it. In this way, if someone else needs to change the expression, the change can be completed without interrupting the function. In addition, if you are not familiar with regular expressions, you may find them difficult to debug.

To avoid these problems, do not use regular expressions when the simpler built-in functions are enough to solve the problem.

POSIX and PCRE

PHP supports the Implementation of two Regular expressions: Portable Operating System Implementation (POSIX) and Perl-Compatible Regular Expression (PCRE ). These two implementations provide different features, but they are just as easy to use in PHP. The regex style you use depends on your past experience and usage habits in regex. There is some evidence that the speed of the PCRE expression is slightly faster than that of the POSIX expression, but in most applications, this difference is not so obvious.

In this example, the syntax of each regex method is included in the comment. In function syntax, regex is the regex parameter, and the searched string is string. Parameters in brackets are optional. Because this tutorial mainly introduces the basic content, it does not provide an introduction to all optional parameters.

1

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.