PHP Regular function

Source: Internet
Author: User
Tags ereg posix expression engine

There are two sets of regular expression function libraries in PHP. The set is provided by the Pcre (Perl Compatible Regular Expression) library. The Pcre library uses the same syntax rules as Perl to implement pattern matching for regular expressions using functions named "Preg_". Another set is provided by the POSIX (Portable Operation System Interface) extension Library. The regular expression for POSIX extensions is defined by POSIX 1003.2, which generally uses a function named "Ereg_" as a prefix.
The functions of the two libraries are similar and the execution efficiency is slightly different. In general, the efficiency of using the Pcre Library is slightly superior to achieving the same functionality. Here is a detailed description of how to use it.


matching of 6.3.1 regular expressions
1. Preg_match ()
Function prototype: 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 the $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 cell in parentheses, and so on. This function only matches once, and eventually returns the number of matches for 0 or 1. Code 6.1 shows a code example for the Preg_match () function.
Code 6.1 date-time matching

Copy CodeThe code is as follows:
<?php
A matching string is required. The DATE function returns the current time
$content = "Current date and Time was". 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 the pattern of time is obvious, it can be easily matched.
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 that the current system time is "August 17, 2006 13:25", the output will be as follows.
The time to 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 POSIX extended library expression. Eregi () is a case-insensitive version of the Ereg () function. The two functions are similar to Preg_match, but the function returns a Boolean value that indicates whether the match was successful or not. It is necessary to note that the first parameter of the POSIX extension library function accepts a regular expression string, that is, it does not require a delimiter. For example, code 6.2 is a method for file name security checks.
Code 6.2 Security checks for file names

Copy CodeThe code is as follows:
<?php
$username = $_server[' Remote_user ');
$filename = $_get[' file '];
Filter the file names to keep the system secure
if (!ereg (' ^[^./][^/]*$ ', $userfile))
{
Die (' This is not an illegal filename! ‘);
}
Filtering a user's name
if (!ereg (' ^[^./][^/]*$ ', $username))
{
Die (' This is not an invalid user name ');
}
Flattening file paths through secure filtering
$thefile = "/home/$username/$filename";
?>


Typically, using a Perl-compatible regular expression matching function, Perg_match (), will be faster than using Ereg () or eregi (). It is recommended to use the STRSTR () or Strpos () function if you are looking for a substring that contains only one string.
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 () also matches only once. The example given in code 6.3 simply illustrates the use of the Preg_grep () function.
Code 6.3 array Query match

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


6.3.2 Global Regular expression matching
1. Preg_match_all ()
Similar to the Preg_match () function. If a third parameter is used, all possible matching results are put in. This function returns the number of times the entire pattern matches (possibly 0) and returns False if an error occurs. The following is an example of converting URL link addresses in text to 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 the text to HTML

Copy CodeThe code is as follows:
<?php
Function: Convert the link address in text to HTML
Input: String
Output: String
function url2html ($text)
{
Match a URL until a blank is present
Preg_match_all ("/http:\/\/?[ ^\s]+/i ", $text, $links);
Set page display URL address length
$max _size = 40;
foreach ($links [0] as $link _url)
{
Calculates the length of the URL. If the setting exceeds $max_size, 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;
}
Running an 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. Multi-line matching
It is difficult to perform complex matching operations by using only regular table functions under POSIX. For example, the entire file, especially multiple lines of text, is matched to find. 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 multi-line matching of file contents

Copy CodeThe code is as follows:
<?php
$rows = File (' php.ini '); Read the php.ini file into the array
Looping through
foreach ($rows as $line)
{
If (Trim ($line))
{
Writes a matching successful parameter 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 parses the *.ini file directly into a large array.
6.3.3 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 the $string for the pattern string $pattern and replaces the matched result with $replacement. When a pattern cell (or sub-mode) is included in a $pattern, the position of the $replacement in the form "\1" or "$" is replaced by the contents of those sub-patterns that are matched in turn. "+" or "$" means the contents of the entire matching string. It is important to note that the backslash is used as an escape character in double quotes, so you must use the form "\\0", "\\1".
Eregi_replace () and ereg_replace () are functionally consistent, except that the former ignores case. Code 6.6 is an applied example of this function, which demonstrates how to do a simple cleanup of the program source code.
Code 6.6 Cleanup of source code

Copy CodeThe code is as follows:
<?php
$lines = File (' source.php '); Read the file into the array
for ($i =0; $i <count ($lines); $i + +)
{
Remove comments that begin with "\ \" or "#" at the end of the line
$lines [$i] = Eregi_replace ("(\/\/|#). *$", "", $lines [$i]);
Eliminate whitespace at the end of a line
$lines [$i] = Eregi_replace ("[\n\r\t\v\f]*$", "\ r \ n", $lines [$i]);
}
Finishing output to Page
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 parameters can be used arrays, the fourth parameter $limit can set the number of substitutions, the default is to replace all. Code 6.7 is an application instance of an array substitution.
Code 6.7 Array Substitution

Copy CodeThe code is 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",
"[Email protected]",
);
Output mode substitution results
Print Preg_replace ($patterns, $replacements, $string);
?>


The output results are as follows.
Name:thomas Ching ",
Email: [Email protected]
Address:no.5, Wilson St., New York, U.S.A
The pattern modifier "E" can be used in preg_replace regular expressions. The effect is to use the match result as an expression, and it can be re-calculated. For example:

Copy CodeThe code is 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 mode variable \\2 will be strtolower converted to lowercase characters
$html _body);
?>


Tips
The Preg_replace function uses Perl-compatible regular expression syntax, which is often a faster alternative than ereg_replace. If you simply replace the string, you can use the Str_replace function.
6.3.4 the splitting of regular expressions
1. Split () and Spliti ()
Function prototype: Array split (String $pattern, string $string [, int $limit])
This function returns an array of strings, each of which is $string by a regular expression $pattern as a substring of the boundary. If $limit is set, the returned array contains a maximum of $limit cells. And the last unit contains all the rest of the $string. Spliti is the ignore size version of Split. Code 6.8 is a frequently used example of a date.
Code 6.8 split of date

Copy CodeThe code is 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 a different 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 CodeThe code is as follows:
<?php
$seek = Array ();
$text = "I have a dream, one day I can make it. So just does it, nothing is impossible! ";
Split the string in blank, punctuation (with a space after each punctuation)
$words = Preg_split ("/[.,;! \s ']\s*/', $text);
foreach ($words as $val)
{
$seek [Strtolower ($val)] + +;
}
echo "Total approx.". Count ($words). "A word." ";
echo "Shared". $seek [' I ']. "One word" I ". ";
?>


Tips
The Preg_split () function uses Perl-compatible regular expression syntax, which is often a faster alternative than split (). Use the regular expression method to split the string, and you can use a wider range of delimited characters. For example, the above analysis of date format and word processing. If you are splitting with only a specific character, we recommend that you use the explode () function, which does not call the regular expression engine, so the speed is the fastest.

PHP Regular function

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.