Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand regular expressions in php

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to understand regular expressions in php. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

In php, a regular expression is a logical formula for manipulating a string, which is a "regular string" formed by predefined specific characters and a combination of these specific characters. This "regular string" can be used to express a filtering logic for a string. Regular expression is an important and complex technique when dealing with text data.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

What is a regular expression

A regular expression is a logical formula for manipulating a string, that is, a "regular string" is formed by predefined specific characters and a combination of these specific characters. This "regular string" is used to express a filtering logic for a string. For example, when registering users, users are often required to fill in data with specific formats such as E-mail, phone number, birthday and so on. In order to prevent users from filling in some invalid data randomly, regular expressions are used.

Regular expression, also known as pattern expression, has a very complete syntax system that can write patterns, which provides a flexible and intuitive method of string processing. By constructing patterns with specific rules, regular expressions are compared with the input string information, and are used in specific functions to achieve string matching, search, replacement and segmentation.

For example, in our daily life, if you want to search for all txt files in a directory on your computer, you can type * .txt in that directory, and then press enter to list all txt files in the directory. The * .txt used here can be understood as a simple regular expression.

Here are two examples built using the syntax of regular expressions, as follows:

/ http (s)?:\ /\ / [\ w.] + [\ w\ /] * [\ w.] *? [\ w &\ +\%] * / is / / matches the regular expression of the URL URL / ^\ w {3,} @ ([Amurz] {2} 7} | [0-9] {3}). (com | cn) $/ / matches the regular expression of the email address

Don't be intimidated by the seemingly garbled strings in the above example, which are built according to the grammatical rules of regular expressions, a string made up of ordinary characters and characters with special functions. And you have to put these strings in specific regular expression functions to have an effect.

The main purpose of using regular expressions is to achieve powerful functionality in a simple way. In order to be simple, effective and powerful, it causes the complexity of regular expression rules, and it is more difficult to build correct and effective regular expressions, so it takes some efforts to learn regular expressions well.

The purpose of regular expressions

A regular expression describes a string matching pattern that can be used to check whether a string contains a certain substring, replace a matching substring, or extract a substring from a string that meets a certain condition, and so on. For example, when a user submits a form, it is obviously not enough to use ordinary literal-based characters to determine whether the phone number, E-mail address, and so on entered are valid.

A regular expression is a text pattern that consists of ordinary characters (such as characters a to z) and special characters (called metacharacters). The regular expression acts as a template that matches a character pattern with the searched string. The pattern of a regular expression can be a single character, a character collection, a character range, a choice between characters, or any combination of all these components.

The purpose of using regular expressions is to achieve powerful functionality in a simple way. In order to be simple, effective and powerful, the rules of regular expressions are complex, and it is more difficult to build correct and effective regular expressions, so we need to make some efforts. After getting started, through a certain reference and a lot of practice, it is more effective and interesting to use regular expressions in development practice.

Using regular expressions in PHP

PHP has two sets of regular expression processing operations supported by the function library:

One is a set of regular expression functions provided by the PCRE (Perl Compatible Regular Expression) library and compatible with the Perl language, with preg_ as the prefix name of the function

The other is the POSIX (Portable Operating System Interface) extension syntax regular expression function, with ereg_ as the prefix of the function.

The functions of the two libraries are similar, but the execution efficiency of PCRE is higher than that of POSIX, so we will only introduce the PCRE library.

The above is how to understand regular expressions in php. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report