Home > Article > Backend Development > Part One of PHP and Regular Expressions Series: Regular Expressions in PHP_PHP Tutorial
Starting today, I will start a series of PHP tutorial articles. Mainly focusing on regular expressions.
The approximate content arrangement is as follows:
1. Regular expressions in PHP
2. Eight practical PHP regular expressions
3. How to write PHP regular expressions that are easier to read
4. Master regular expressions in half an hour
5. Application of regular expressions in article collection systems and FAQs
6. ...More planning is in progress
As for the source of the article content, some are reorganized from old articles on this site, some are translated from English literature (thanks! oEL students in Canada), and some It's a personal experience.
My personal ability is limited, so there must be some mistakes. Friends who find them please give reminders and correct them in time. It will not mislead novices. If the article can give everyone some reference, I will be very satisfied.
PHP and regular expression series: Regular expressions in PHP
Introduction to regular expressions and the role of regular expressions in PHP
Regular expression is a A way to represent rules that allow you to flexibly match, check, replace, and string strings in PHP. This article covers the basics of PCRE and how to use the preg_match(), preg_replace(), and preg_split() functions.
Next, let us learn how to use these functions step by step from examples.
Rule matching preg_match
Using preg_match(), we can complete the rule matching of strings. The preg_match() function returns 1 if a match is found, 0 otherwise. There is an optional third parameter that allows you to store the matched parts in an array. This feature can become very useful when validating data.
$string = "football";
if (preg_match('/foo/', $string)) {
// Correct match
}
The above example will match successfully because the word football contains foo. Now let's try something more complex, such as validating an email address.
$string = "first.last@domain.uno.dos";
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)) {
// Verify Email Address
}
This example will verify This email address must be in the correct format. Now let's take a look at the various rules represented by this regular expression.
PCRE, as the name suggests, has the same syntax as regular expressions in Perl, so each regular expression must have a pair of delimiters. We generally use / as the delimiter.
The leading ^ and trailing $ tell PHP to check from the beginning to the end of the string. Without the $, the program will still match to the end of the Email.
[ and ] are used to limit the allowed input types. For example a-z allows all lowercase letters, A-Z allows all uppercase letters, 0-9 all numbers, etc., and many more.
{ and } are used to limit the number of characters expected. For example {2,4} means that each section of the string can be 2-4 characters long, such as .com.cn or .info. Here, "." does not count as a character, because the allowed input type defined before {2,4} only has uppercase and lowercase letters, so this paragraph only matches uppercase and lowercase letters
( and) are used to merge sections , and defines the characters that must be present in the string. (a|b|c) matches a or b or c.
(.) will match all characters, while [.] will only match "." itself.
To use some symbols themselves, you must add a in front. These characters are: ( ) [ ] . * ? + ^ | $
Rule replacement preg_replace
preg_replace allows you to replace strings that match a regular expression you define. A simple comment removal function:
preg_replace('[(/*)+.+(*/)]', '', $val);
This code can be moved Except for multi-line comments in PHP and CSS using the /* comment*/ format. The three parameters are the regular expression, the string to be replaced and the target string to be replaced (the removal function is to be used here, so it is a blank string -> ''). If you want to match secondary rules, you can use $0 to match all, $1, $2, etc., and so on to represent individual secondary rules.
Rule split preg_split
preg_split can split the entire string into multiple segments of 1, 2 or more characters according to the matched regular expression. For example, get tags, whether separated by spaces or commas:
$tags = preg_split('/[,]/', 'my,tags,unevenly,spaced');
print_r($tags);
Regular expressions are a very practical technique that allows you to focus on what you expect.
But sometimes it is very annoying that a regular expression does not allow you to get the expected results as you wish, so I will attach some simple syntax guidelines in the second article of this series, hoping to help to everyone.
Attachment: PCRE syntax guide
/ Delimiter
^ String header
$ String tail
[a-z] All lowercase letters
[A-Z] All uppercase letters
[0-9] All numbers
? Zero or one immediately preceding character
* Zero or more immediately preceding characters
+ One or more characters immediately preceding
{4} 4 characters immediately preceding
{4,8} 4-8 characters immediately preceding
. Any character
( red|green|blue) Red or green or blue (red or green or blue)
s Space
Special characters (need to be added in front)
( ) [ ] . * ? + ^ | $