Home >Backend Development >PHP Tutorial >How to use regular expressions in PHP (1)_PHP Tutorial

How to use regular expressions in PHP (1)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:25:02881browse

What is a regular expression?

A few years ago I did some interesting testing of input fields in web forms. The user will enter a phone number into this form. This phone number will then be printed on the user's ad as they typed it. As required, U.S. phone numbers can be entered in several ways: either (555) 555-5555 or 555-555-5555, but 555-5555 is not accepted.

You may be wondering, why don’t we throw away all non-numeric characters and just ensure that the remaining characters total 10? This approach does work, but it doesn't prevent the user from typing something like !555?333-3333.

From a web developer’s perspective, this situation presents an interesting challenge. I could write routines to check for a variety of different formats, but I'd like to find a solution that allows some flexibility if the user then accepts a format like 555.555.5555.

This is where regular expressions (regex for short) come in handy. I've cut and pasted them into apps before and never found any syntax issues that were difficult to understand. Regex looks a lot like mathematical expressions. When you see an expression of the form 2x2=4, you usually think "2 times 2 equals 4." Regular expressions are very similar. After reading this article, when you see a regular expression like ^b$, you will tell yourself: "The beginning of a line is b, followed by the end of the line." Not only that, you will realize how easy it is to use regular expressions in PHP.

When to use regex

You should use regex to complete search and replace operations when there are rules to follow, but you don’t have to have the exact characters you need to find or replace . For example, in the phone number example mentioned above, the user defines rules that indicate the format of the entered phone number, but not the digits contained in the phone number. This also applies to scenarios with large amounts of user input. U.S. state abbreviations are limited to two uppercase letters from A to Z. Regular expressions can also be used here, allowing you to simply limit text in a form or user input to letters of the alphabet, regardless of case or length issues.

When not to use regex

Regular expressions are powerful, but they also have some flaws. One of them requires skills in reading and writing expressions. If you decide to include regular expressions in your application, they should be fully commented. This way, if someone else needs to change the expression later, they can do so without disrupting functionality. Additionally, if you are not familiar with using regular expressions, you may find them difficult to debug.

To avoid these difficulties, don't use regular expressions when simpler built-in functions solve the problem well enough.

POSIX and PCRE

PHP supports two regular expression implementations: Portable Operating System Implementation (POSIX) and Perl-Compatible Regular Expression (PCRE). The two implementations offer different features, but they are equally simple to use in PHP. The regex style you use depends on your past experience and usage habits with regex. There is some evidence that PCRE expressions are slightly faster than POSIX expressions, but in the vast majority of applications this difference is not that significant.

In the examples in this article, the syntax of each regex method is included in the comments. In function syntax, regex is the regex parameter and the string being searched for is string. The parameters in parentheses are optional. Since this tutorial mainly introduces the basic content, it will not give an introduction to all optional parameters.

1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446714.htmlTechArticleWhat is a regular expression? A few years ago I did some interesting experiments with input fields in web forms. The user will enter a phone number into this form. The phone number is then typed by the user...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn