Starting today, you will start a series of articles on PHP tutorials. It is mainly for regular expressions.
The approximate sort of content is arranged like this:
1. Regular Expressions in PHP
2. Eight useful PHP regular expressions
3. How to write a PHP regular expression that is easier to read
4. Half-hour mastery of regular expressions
5. The application of regular in the collection system of the article and questions and answers
6 ..... More in planning
The article content source aspect, has the old article of this station to reorganize and come, also has translated the English literature to come (thanks to the!oel classmate in Canada), also some is the personal experience.
Personal ability is limited, there must be some errors, find friends please give reminders and when corrections. Not to mislead novices. If the article can give you some reference, it is very satisfied.
PHP and regular expression series: Regular Expressions in PHP
Introduction to regular expressions and the role of regular expressions in PHP
Regular expressions are a way of representing rules that you can use to flexibly match, validate, replace, and string in PHP. This article covers the basics of PCRE, and how to use the Preg_match (), Preg_replace (), and Preg_split () functions.
Next, let's start with the example step-up and learn how to use these functions.
Rule Matching Preg_match
With Preg_match (), we can complete the rule matching of strings. If a match is found, the Preg_match () function returns 1, otherwise 0 is returned. There is also an optional third parameter that allows you to put a matching part in an array. This functionality can become very useful when validating data.
$string = "Football";
if (Preg_match ('/foo/', $string)) {
Match correct
}
The above example will match successfully, because the word football contains foo. Now let's try a more complicated one, such as verifying 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 verifies that the email address is in the correct format. Now let's look at the various rules represented by this regular expression.
PCRE, as the name implies, has the same syntax as regular expressions in Perl, so each regular expression must have a pair of delimiters. We generally use/as delimiters.
Start with ^ and end of $ 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 restrict the type of license input. For example A-Z allows all lowercase letters, a-Z allows all uppercase letters, 0-9 all numbers, and more, among other types.
{and} are used to limit the number of characters expected. For example, {2,4} indicates that each section of a string can have a length of 2-4 characters, such as. com.cn or. Info. Here, "." does not count as a character, because the type of license input defined before {2,4} is only uppercase and lowercase, so the segment matches only the uppercase and lowercase letters
(and) are used to combine subsections and define the characters that must exist in the string. (A|b|c) to match A or B or C.
(.) All characters are matched, and [.] Matches only "." itself.
To use some of the symbols themselves, one must be added first. These characters are: () []. * ? + ^ | $
Rule substitution preg_replace
Preg_replace allows you to replace a regular expression in a string that matches your definition. A simple annotation removal feature:
Preg_replace (' [(/*) +.+ (*/)] ', ', $val);
This code can remove the multiline comment in PHP and CSS using the/* Comment */format. Three of these parameters are regular expressions, the string to replace and the target string to replace (here to remove the function, so it is a blank string--"). If you want to match the secondary rules, you can use $ A to represent all matches, $, $, etc., and so on, representing the respective secondary rules.
Rule Segmentation Preg_split
Preg_split can divide the entire string into multiple segments of 1, 2, or more characters by matching regular expressions. For example, to get a label, whether it is separated by a space or a comma:
$tags = Preg_split ('/[,]/', ' my,tags,unevenly,spaced ');
Print_r ($tags);
Regular expressions are a useful technique that allows you to focus on what you expect to get.
But sometimes a regular expression doesn't make you get what you want, so I'm going to attach some simple grammar guides to the second article in this series to help you.
Attached: PCRE Grammar Guide
/delimiter
^ String Header
$ string Tail
[A-z] all lowercase letters
[A-z] all uppercase letters
[0-9] All numbers
? 0 or a immediately preceding character
* 0 or more immediately preceding characters
+ one or more immediately preceding characters
{4} 4 immediately preceding characters
{4,8} 4-8 immediately preceding characters
. Any character
(Red|green|blue) Red or green or blue
S space
Special characters (need to be added before)
( ) [ ] . * ? + ^ | $
http://www.bkjia.com/PHPjc/363980.html www.bkjia.com true http://www.bkjia.com/PHPjc/363980.html techarticle starting today, we will start a series of articles on the PHP tutorial. Mainly for regular expressions. The approximate content sort is arranged like this: 1. Regular Expressions in PHP 2. Eight useful PHP regular ...