Regular Syntax rules
Let's first introduce three things, and then we can make a simple example.
1. The left slash of the separator "/"
Php uses the left slash as the delimiter, specifying the boundary of the rule and appearing in pairs. The content between the two delimiters is the rule to be matched.
II. Metacharacters-appearance mode
Matching method, which can be used to specify the mode in which the leading character (that is, the character located before the metacharacters) appears in the target object.
For example, if one occurs more than once, it appears multiple times and does not appear. And so on.
Commonly used:
+ 1 or multiple times
* 0 times or once or multiple times
? 0 or 1 time
{2, 6} 2-6 times
{2} happened to be twice
{3 ,}at least 3 times,> = 3
3. Metacharacters --- specific character classes
Represents a specific character
\ S blank characters (single space character, tab key, and line break)
\ S non-blank characters
\ D number (a single 0 to 9)
\ W letters, numbers or underscores
\ W (non-English letters, numbers or underscores)
. All characters except line breaks
1. preg_match () function
Function prototype: int preg_match (string $ pattern, string $ content [, array $ matches])
The preg_match () function searches the $ content string for the content that matches the regular expression given by $ pattern. If $ matches is provided, the matching result is put into it. $ Matches [0] will contain the text that matches the entire pattern, $ matches [1] will contain the content that the first captured matches the pattern unit in brackets, and so on. This function only performs a match and returns the number of matching results of 0 or 1. Code 6.1
The preg_match () function searches strings in the search mode. If yes, true is returned. Otherwise, false is returned.
| The code is as follows: |
Copy code |
|
$ Pattern = '/php /';
$ Str = '111cn. Net ';
Echo preg_match ($ pattern, $ str ); |
Output result: 1
2. preg_grep () function
The preg_grep () function searches for all elements in an array and returns an array consisting of all elements that match a pattern.
| The code is as follows: |
Copy code |
|
$ Pattern = '/p $ /';
$ StrArray = array ('asp ', 'php', 'JSP', 'Python', 'Ruby ');
Print_r (preg_grep ($ pattern, $ strArray )); |
Output result:
Array
(
[0] => asp
[1] => php
[2] => jsp
)
3. preg_match_all () function
The preg_match_all () function matches all occurrences of the pattern in the string, and then puts all the matches into the array.
| The code is as follows: |
Copy code |
|
<? Php
// Preg_match ("regular expression", "string") is used to find matching items in the string.
$ Email = "987044391@qq.com ";
If (preg_match ("/^ ([a-zA-Z0-9]) + ([. a-zA-Z0-9 _-]) * @ ([. a-zA-Z0-9 _-]) + ([. a-zA-Z0-9 _-] +) + ([. a-zA-Z0-9 _-]) $/", $ email )){
Echo 'matched successfully } Else {
Echo 'matching failed }
// Preg_quote ("string") add an escape character (\) before each regular expression syntax \
$ Str = "php point-to-point is a php learning website )... ";
Echo preg_quote ($ str );
Echo "
// Preg_split ("regular", "string") splits the string
$ Php = "+ php ++ click-through ++ is a good website ";
$ Field = preg_split ("/\ + {1,}/", $ php );
Foreach ($ field as $ f ){
Echo $ f ."";
}
Echo "// Preg_grep ("regular", "string") matches the array and returns the new array
$ Phpddt = array ("php dot connect", "php100", "haha", "hahahaha", "phpchina ");
$ Item = preg_grep ("/^ php/", $ phpddt );
Print_r ($ item );
Echo "// Preg_replace ("regular", "Replace content", "original string") is very important and frequently used.
$ A = "Welcome to http://www.111cn.net/"; // add a hyperlink to the beginning of http
Echo preg_replace ("/http :\/\/(. *) \ // "," <a href = \ "\$ {0 }\" >\0 {0} </a> "," $ ");
?> |
4. preg_quote () function
The preg_quote () function inserts a backslash before each character with special meanings in the regular expression syntax.
Echo preg_quote ('Apple Price: $5/kg ');
Output: The price of Apple is \ $5/kg.
5. preg_replace () function
The preg_replace () function searches for all the matches and replaces them with the desired string to return
| The code is as follows: |
Copy code |
|
$ Pattern = '/php [1-5]/';
$ Str = 'php1php2php3php4php5php6php7php8 ';
Echo preg_replace ($ pattern, 'php', $ str ); |
Output result: phpphpphpphpphp6php7php8
6. preg_split () function
The preg_split () function separates strings in a case-insensitive manner. It groups the obtained strings into an array and returns
| The code is as follows: |
Copy code |
|
$ Pattern = '/[\. @]/';
$ Str = 'Feng. Yingyuan@sina.com.cn ';
Print_r (preg_split ($ pattern, $ str ));
Output result:
Array
(
[0] => feng
[1] => yingyuan
[2] => sina
[3] => com
[4] => cn
)
|
Ereg () and eregi ()
Ereg () is a matching function of regular expressions in the POSIX Extension Library. Eregi () is a case-insensitive version of the ereg () function. The two functions are similar to the preg_match function, but the function returns a boolean value, indicating whether the matching is successful or not. It should be noted that the first parameter of the POSIX extension library function accepts a regular expression string, that is, it does not need to use the delimiter. For example, code 6.2 is a file name security check method.
Code 6.2 file name security check
| The code is as follows: |
Copy code |
|
<? Php
$ Username = $ _ SERVER ['remote _ user'];
$ Filename = $ _ GET ['file'];
// Filter file names to ensure system security
If (! Ereg ('^ [^./] [^/] * $', $ userfile ))
{
Die ('this is not an invalid file name! ');
}
// Filter user names
If (! Ereg ('^ [^./] [^/] * $', $ username ))
{
Die ('This is not an invalid username ');
}
// Combine file paths through security filtering
$ Thefile = "/home/$ username/$ filename ";
?> |
Generally, using a Perl-compatible regular expression to match the function perg_match () is faster than using ereg () or eregi. We recommend that you use the strstr () or strpos () function
Preg_replace ()
Function prototype: mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit])
Preg_replace is more powerful than ereg_replace. Arrays can be used for the first three parameters. The fourth parameter $ limit can be used to set the number of replicas. By default, all replicas are replaced. Code 6.7 is an array replacement application instance.
| The code is as follows: |
Copy code |
|
<? Php
// String
$ String = "Name: {Name} <br>/nEmail: {Email} <br>/nAddress: {Address} <br>/n ";
// Mode
$ Patterns = array (
"/{Address }/",
"/{Name }/",
"/{Email }/"
);
// Replace the string
$ Replacements = array (
"No. 5, Wilson St., New York, U. S. ",
"Thomas Ching ",
"Tom@emailaddress.com ",
);
// Output mode replacement result
Print preg_replace ($ patterns, $ replacements, $ string );
?> |