PHP Regular expression Common function use summary _ Regular expression

Source: Internet
Author: User
Tags ereg html tags lowercase mixed php regular expression posix expression engine
In PHP There are two sets of regular expression libraries. A set is provided by the Pcre (Perl compatible Regular Expression) library. The Pcre library implements pattern matching for regular expressions using the same syntax rules as Perl, using functions named "Preg_" as prefixes. The other is provided by the POSIX (Portable Operation System Interface) extension Library. POSIX-extended regular expressions are defined by POSIX 1003.2 and typically use functions named "Ereg_" as prefixes.
The functions of the two sets of functions are similar and the execution efficiency is slightly different. In general, to achieve the same functionality, the use of the Pcre library has a slightly more efficient advantage. The following is a detailed description of how to use it.
matching of 6.3.1 regular expressions
1. Preg_match ()
function prototypes: int preg_match (String $pattern, String $content [, array $matches])
The Preg_match () function searches the $content string for content that matches the regular expression given by $pattern. If $matches is provided, the matching result is placed in it. $matches [0] will contain text that matches the entire pattern, $matches [1] will contain the first captured content that matches the pattern unit in parentheses, and so on. The function only makes one match, and eventually returns the number of matches of 0 or 1. Code 6.1 gives a sample code for the Preg_match () function.
Code 6.1 matching Date-time
Copy Code code as follows:

<?php
A matching string is required. The DATE function returns the current time
$content = "Current date and". Date ("y-m-d h:i a"). ", we are learning PHP together.";
Use the usual method to match the time
if (Preg_match ("/\d{4}-\d{2}-\d{2} \d{2}:\d{2} [ap]m/", $content, $m))
{
echo "matches the time:". $m [0]. "\ n";
}
Because of the pattern of time is obvious, can also simple match
if (Preg_match ([\d-]{10}) ([\d:]{5} [Ap]m)/", $content, $m))
{
echo "Current date is:". $m [1]. "\ n";
echo "Current time is:". $m [2]. "\ n";
}
?>

This is a simple dynamic text string matching instance. Assuming the current system time is "August 17, 2006 13:25", the following will be output.
The time of the match is: 2006-08-17 01:25 pm
The current date is: 2006-08-17
The current time is: 01:25 pm
2. Ereg () and eregi ()
Ereg () is a matching function of the regular expression in the POSIX extension library. Eregi () is the Ereg () function's ignored case version. They are similar to the Preg_match function, but the function returns a Boolean value indicating whether the match was successful or not. It is to be explained that the first parameter of the POSIX extended library function accepts a regular expression string, that is, the use of a delimiter is not required. For example, code 6.2 is a method of file name security checking.
Code 6.2 Security Check for file names
Copy Code code as follows:

<?php
$username = $_server[' Remote_user '];
$filename = $_get[' file '];
Filter file names to ensure system security
if (!ereg (' ^[^./][^/]*$ ', $userfile))
{
Die (' This is not an illegal filename! ');
}
Filter the user name
if (!ereg (' ^[^./][^/]*$ ', $username))
{
Die (' This is not an invalid username ');
}
Flatten file paths through security filtering
$thefile = "/home/$username/$filename";
?>

Typically, using Perl-compatible regular expression matching functions perg_match () will be faster than using Ereg () or eregi (). If you are simply looking for a string that contains a substring, it is recommended that you use the STRSTR () or Strpos () function.
3. Preg_grep ()
Function prototypes: Array preg_grep (string $pattern, array $input)
The Preg_grep () function returns an array that includes the cells in the $input array that match the given $pattern pattern. For each element in the input array $input, Preg_grep () is matched only once. The example given in code 6.3 simply illustrates the use of the Preg_grep () function.
Code 6.3 Array Query matching
Copy Code code as follows:

<?php
$subjects = Array (
"Mechanical Engineering", "Medicine",
"Social Science", "agriculture",
"Commercial Science", "politics"
);
Match all account names that consist of only one word
$alonewords = Preg_grep ("/^[a-z]*$/i", $subjects);
?>

6.3.2 for global Regular expression matching
1. Preg_match_all ()
Similar to the Preg_match () function. If the third argument is used, all possible matches are put into. This function returns the number of times the entire pattern match (possibly 0), if error returns false. The following is an example of converting a URL link address in text to an HTML code. Code 6.4 is an example of the use of the Preg_match_all () function.
Code 6.4 Convert the link address in text to HTML
Copy Code code as follows:

<?php
Function: Convert the link address in text to HTML
Input: String
Output: String
function url2html ($text)
{
Match a URL until there is a blank
Preg_match_all ("/http:\/\/?[ ^\s]+/i ", $text, $links);
Set the length of the page to display the URL address
$max _size = 40;
foreach ($links [0] as $link _url)
{
Calculates the length of the URL. If the $max_size setting is exceeded, it is shortened.
$len = strlen ($link _url);
if ($len > $max _size)
{
$link _text = substr ($link _url, 0, $max _size). " ...";
} else {
$link _text = $link _url;
}
Generate HTML text
$text = Str_replace ($link _url, "<a href= ' $link _url ' > $link _text</a>", $text);
}
return $text;
}
Run instance
$str = "This is a multiline text that contains multiple URL link addresses. Welcome to visit Http://www.jb51.net ";
Print url2html ($STR);
/* Output Results
This is a multiline text that contains multiple URL link addresses. Welcome to <a href= ' http://www.jb51.net ' >
Http://www.jb51.net</a>
*/
?>

2. Multiple-line matching
It is difficult to perform complex matching operations using only regular table functions under POSIX. For example, a matching lookup is made for an entire file, especially multiple lines of text. One way to do this with Ereg () is by branch processing. The example in code 6.5 shows how Ereg () assigns the parameters of the INI file to the array.
Code 6.5 multiple-line matching of file contents
Copy Code code as follows:

<?php
$rows = File (' php.ini '); Read the php.ini file to the array
Loop traversal
foreach ($rows as $line)
{
If (Trim ($line))
{
Writes the parameters that match successfully to the array
if (eregi ("^ [a-z0-9_.] *) *= (. *) ", $line, $matches))
{
$options [$matches [1]] = Trim ($matches [2]);
}
Unset ($matches);
}
}
Output parameter Results
Print_r ($options);
?>

Tips
This is just for the convenience of explaining the problem. The best way to parse a *.ini file is to use the function Parse_ini_file (). This function resolves the *.ini file directly into a large array.
6.3.3 the replacement of regular expressions
1. Ereg_replace () and eregi_replace ()
Function Prototypes: String ereg_replace (String $pattern, String $replacement, String $string)
String Eregi_replace (String $pattern, String $replacement, String $string)
Ereg_replace () Searches for the pattern string $pattern in $string and replaces the matched result with $replacement. When the $pattern contains a modal unit (or child mode), the positions in the $replacement, such as "\1" or "$", are replaced in turn by the content matched by those child patterns. and "Yes" or "$" refers to the contents of the entire matching string. It is important to note that in double quotes the backslash is used as an escape character, so you must use the form "\\0", "\\1".
The functionality of Eregi_replace () and ereg_replace () is consistent, except that the former ignores case. Code 6.6 is an example of the application of this function, which demonstrates how to do a simple cleanup of the program's source code.
Code 6.6 Cleanup of source code
Copy Code code as follows:

<?php
$lines = File (' source.php '); Reading a file into an array
for ($i =0; $i <count ($lines); $i + +)
{
Remove a comment that starts at the end of the line with "\" or "#"
$lines [$i] = Eregi_replace ("(\/\/|#). *$", "", $lines [$i]);
Eliminate the white space at the end of the line
$lines [$i] = Eregi_replace ("[\n\r\t\v\f]*$", "\ r \ n", $lines [$i]);
}
Output to page after finishing
Echo Htmlspecialchars (Join ("", $lines));
?>

2. Preg_replace ()
Function prototypes: Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
Preg_replace is more powerful than ereg_replace. The first three arguments can use an array, and the fourth argument $limit can set the number of replacements, and the default is replace all. Code 6.7 is an application instance of an array substitution.
Code 6.7 Array Substitution
Copy Code code as follows:

<?php
String
$string = "Name: {name}<br>\nemail: {email}<br>\naddress: {address}<br>\n";
Mode
$patterns =array (
"/{address}/",
"/{name}/",
"/{email}/"
);
Replace string
$replacements = Array (
"No.5, Wilson St., New York, U.S.A",
"Thomas Ching",
"Tom@emailaddress.com",
);
Output mode substitution results
Print Preg_replace ($patterns, $replacements, $string);
?>

The output is as follows.
Name:thomas Ching ",
Email:tom@emailaddress.com
Address:no.5, Wilson St., New York, U.S.A
You can use the pattern modifier "E" in preg_replace regular expressions. The effect is to use the matching result as an expression, and you can do the operation again. For example:
Copy Code code as follows:

<?php
$html _body = "The HTML tags in the output will all be lowercase letters
Echo Preg_replace (
"/(<\/?) (\w+) ([^>]*>)/e ",
"' \\1 '. Strtolower (' \\2 ')." \\3 ' ",//here the pattern variable \\2 will be strtolower converted to lowercase characters
$html _body);
?>

Tips
The Preg_replace function uses Perl-compatible regular expression syntax, which is usually a faster alternative than ereg_replace. If you are simply replacing a string, you can use the Str_replace function.
6.3.4 the split of regular expressions
1. Split () and Spliti ()
Function prototypes: Array split (String $pattern, string $string [, int $limit])
This function returns an array of strings, each of which is a substring separated by a regular expression $pattern as a boundary $string. If $limit is set, the returned array contains a maximum of $limit cells. And the last of these cells contains all the remaining parts of the $string. Spliti is a split, ignore size version. Code 6.8 is an example that is often used in relation to dates.
Split of Code 6.8 dates
Copy Code code as follows:

<?php
$date = "08/30/2006";
The delimiter can be a slash, a dot, or a horizontal line
List ($month, $day, $year) = Split (' [/.-] ', $date);
Output to another time format
echo "Month: $month; Day: $day; Year: $year <br/>\n ";
?>

2. Preg_split ()
This function is consistent with the function of the split function. Code 6.9 is an example of finding the number of words in an article.
Code 6.9 Finding the number of words in an article
Copy Code code as follows:

<?php
$seek = Array ();
$text = "I have a dream" I can make it. So just does it, nothing is impossible! ";
Blank the string, punctuation marks (you may also have spaces after each punctuation)
$words = Preg_split ("/[.,;! \s ']\s*/', $text);
foreach ($words as $val)
{
$seek [Strtolower ($val)] + +;
}
echo "Total approximately". Count ($words). "A word. ";
echo "in common". $seek [' I ']. "The word" I ". ";
?>

Tips
The Preg_split () function uses Perl-compatible regular expression syntax, and is usually a faster alternative than split (). Using the method of regular expressions to split strings, you can use a wider range of separator characters. For example, the above analysis of date format and word processing. If you are splitting with only a particular character, it is recommended that you use the explode () function, which does not call the regular expression engine, so the speed is the fastest.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.