The following is a collection of common function codes for regular expressions in PHP to facilitate learning PHP regular expression
preg_grep.
(PHP 4, PHP 5)
preg_grep -- Returns array cell descriptions matching pattern
array preg_grep ( string pattern, array input [, int flags] )
preg_grep() Returns an array containing the cells in the input array that match the given pattern pattern.
flags can be the following flags:
PREG_GREP_INVERT
If passed in this flag, preg_grep() returns cells in the input array that do not match the given pattern. This tag is available since PHP 4.2.0.
As of PHP 4.0.4, the results returned by preg_grep() are indexed using the keys from the input array. If you do not want such results, use array_values() to reindex the results returned by preg_grep().
The above is the description of preg_grep() in the manual. First of all, this is a perl-compatible regular function, so I guess preg_grep means p(perl)reg(regular)_grep. Its characteristic is that it can be used for arrays. Through its own expansion, it can be used for regular matching in multi-dimensional arrays, and it can Return a matching or non-matching array through the flags parameter. Its efficiency is much faster than using the foreach(...){if...} structure (not verified), and it can match complex patterns. It is of great use in applications such as search and sorting.
example:
<?
$arr = array('abc'=>12.213,'bb'=>12345,'ba'=>23.2321,34.3,'23'=>'3.3','23434'=>'bbb');
// Return all array elements containing floating point numbers.
$fl_array = preg_grep ("/^(d+)?.d+$/", $arr);
print_r($fl_array);
?>
preg_match
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
preg_match -- Regular expression matching instructions
int preg_match (string pattern, string subject [, array matches [, int flags]] )
searches the subject string for content that matches the regular expression given by pattern.
If matches are provided, it will be populated with the results of the search. $matches[0] will contain text that matches the entire pattern, $matches[1] will contain text that matches the first captured subpattern in parentheses, and so on.
flags can be the following flags:
PREG_OFFSET_CAPTURE
If this flag is set, the associated string offset will also be returned for each occurrence of a match. Note that this changes the value of the returned array so that each cell in it is also an array, where the first item is the matched string and the second item is its offset. This tag is available since PHP 4.3.0.
The flags parameter is available since PHP 4.3.0.
preg_match() returns the number of times pattern is matched. Either 0 times (no match) or 1 time, since preg_match() will stop searching after the first match. preg_match_all(), on the contrary, will search until the end of subject. If an error occurs preg_match() returns FALSE.
Tip: If you just want to see if one string is contained within another string, don't use preg_match(). You can use strpos() or strstr() instead, which is much faster.
The above is the description of preg_match() in the manual. I think the function of this function is that it can be used for verification, that is, whether a certain string meets certain requirements. The limitation is that as mentioned above, it either matches 0 times or 1 time. And the return value is the number of matches. When a full match is required, preg_match_all() can be used. It is also worth mentioning the role of the $matches array, which can be used as the return value of the self-pattern, which is sometimes useful.
Example:
<?
if (preg_match ("/(bwebb)s(d)/i", "PHP is the web 45 scripting web 34 language of choice.",$match)) {
print "A match was found.";
print_r($match);
} else {
print "A match was not found.";
}
?>
<?php
// Get hostname from URL
preg_match("/^(http://)?([^/]+)/i",
" http://www.php.net/index.html ", $matches);
$host = $matches[2];
// Get the next two segments from the host name
preg_match("/[^./]+.[^./]+$/", $host, $matches);
echo "domain name is: {$matches[0]}n";
?>
preg_match_all
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
preg_match_all -- Perform global regular expression matching. The explanation of this function in the manual is very clear, so I won't explain it further.
illustrate
int preg_match_all (string pattern, string subject, array matches [, int flags])
searches for all content in subject that matches the regular expression given by pattern and puts the results into matches in the order specified by flags.
After the first match is found, subsequent searches start at the end of the previous match.
flags can be a combination of the following flags (note that it does not make sense to use PREG_PATTERN_ORDER and PREG_SET_ORDER together):
PREG_PATTERN_ORDER
Sort the results so that $matches[0] is an array of all pattern matches, $matches[1] is an array of strings matched by the subpattern in the first bracket, and so on.
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U", "<b>example: </b><div align=left>this is a test</div >",
$out, PREG_PATTERN_ORDER);
print $out[0][0].", ".$out[0][1]."n";
print $out[1][0].", ".$out[1][1]."n";
?>
This example will output:
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
Therefore, $out[0] contains the string matching the entire pattern, and $out[1] contains the string between a pair of HTML tags.
PREG_SET_ORDER
Sort the results so that $matches[0] is the array of the first set of matches, $matches[1] is the array of the second set of matches, and so on.
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
"<b>example: </b><div align=left>this is a test</div>",
$out, PREG_SET_ORDER);
print $out[0][0].", ".$out[0][1]."n";
print $out[1][0].", ".$out[1][1]."n";
?>
This example will output:
<b>example: </b>, example:
<div align=left>this is a test</div>, this is a test
In this example, $matches[0] is the first set of matching results, $matches[0][0] contains the text matching the entire pattern, $matches[0][1] contains the text matching the first sub-pattern, ending with And so on. Likewise, $matches[1] is the second set of matches, and so on.
PREG_OFFSET_CAPTURE
If this flag is set, the associated string offset will also be returned for each occurrence of a match. Note that this changes the value of the returned array so that each cell is also an array, where the first item is the matched string and the second item is its offset within the subject. This tag is available since PHP 4.3.0.
If no tag is given, PREG_PATTERN_ORDER is assumed.
Returns the number of times the entire pattern was matched (possibly zero), or FALSE on error.
Example 1. Get all phone numbers from a text
<?php
preg_match_all ("/(? (d{3})? )? (?(1) [-s] ) d{3}-d{4}/x",
"Call 555-1212 or 1-800-555-1212", $phones);
?>
Example 2. Search for matching HTML tags (greedy)
<?php
// \2 is an example of a reverse reference, its meaning in PCRE is
// Must match the content within the second set of brackets in the regular expression itself, in this case
// That's ([w]+). Because the string is in double quotes, you need
//Add an extra backslash.
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all ("/(<([w]+)[^>]*>)(.*)(</\2>)/", $html, $matches);
for ($i=0; $i< count($matches[0]); $i++) {
echo "matched: ".$matches[0][$i]."n";
echo "part 1: ".$matches[1][$i]."n";
echo "part 2: ".$matches[3][$i]."n";
echo "part 3: ".$matches[4][$i]."nn";
}
?>
preg_quote
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
preg_quote -- escape regular expression character description
string preg_quote ( string str [, string delimiter] )
preg_quote() takes str as a parameter and adds a backslash in front of each character that belongs to the regular expression syntax. If you need to match a dynamically generated string as a pattern, you can use this function to escape the special characters it may contain.
If the optional argument delimiter is provided, this character will also be escaped. It can be used to escape the delimiter required by the PCRE function. The most commonly used delimiter is the slash /.
Special characters for regular expressions include: . + * ? [ ^ ] $ ( ) { } = ! < > | :.
Note: This function is safe to use on binary objects.
The above is the explanation in the manual, which is very clear, so I won’t go into details. In addition, there is a note in the manual that this function can be used safely for binary objects, which is very useful.
Example: Example 1. preg_quote() example
<?php
$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
echo $keywords; // returns $40 for a g3/400
?>
Example 2. Add italics to a word in a text
<?php
// In this example, preg_quote($word) is used to keep the asterisk out of the regular expression
// Has a special meaning.
$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/".preg_quote($word)."/",
"<i>".$word."</i>",
$textbody);
?>
The next step is to apply the preg_replace function that is super flexible, super powerful, and super widely used.
preg_replace
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
preg_replace – performs regular expression search and replace instructions
mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
searches the subject for matches of the pattern pattern and replaces them with replacement. If limit is specified, only limit matches will be replaced. If limit is omitted or has a value of -1, all matches will be replaced.
replacement can contain a backreference in the form \n or (since PHP 4.0.4) in the form $n, with the latter being preferred. Each such reference will be replaced by text matching the nth captured bracketed subpattern. n can range from 0 to 99, where \0 or $0 refers to the text matched by the entire pattern. Count the left parenthesis from left to right (starting at 1) to get the number of subpatterns.
For replacement patterns that follow a backreference by a number (that is, a number that immediately follows a matching pattern), the familiar \1 notation cannot be used to represent the backreference. For example, \11 will make preg_replace() confused whether it wants a backreference of \1 followed by the number 1 or a backreference of \11 . The solution in this case is to use ${1}1. This creates an isolated backreference for $1, leaving the other 1 just a literal.
If a match is found, the replaced subject will be returned, otherwise the original subject will be returned.
Each parameter of preg_replace() (except limit) can be an array. If both pattern and replacement are arrays, they will be processed in the order in which their keys appear in the array. This is not necessarily the same as the numerical order of the index. If an index is used to identify which pattern is to be replaced by which replacement, the array should be sorted with ksort() before calling preg_replace().
If subject is an array, a search and replace is performed on each item in subject and an array is returned.
If pattern and replacement are both arrays, preg_replace() will take out the values from them in order to search and replace the subject. If there are fewer values in replacement than in pattern, an empty string is used as the remaining replacement value. If pattern is an array and replacement is a string, this string is used as the replacement value for each value in pattern. The other way around is meaningless.
The /e modifier causes preg_replace() to treat the replacement argument as PHP code (after appropriate backreferences have been replaced). Tip: Make sure that replacement forms a valid PHP code string, otherwise PHP will report a syntax parsing error on the line containing preg_replace().
Note: The limit parameter was added after PHP 4.0.1pl2.
I think its power is that it can not only handle strings, but also arrays, and its reverse reference function is very flexible. Basically, it can meet most of the needs of ordinary users. If it is not competent, then we also have the preg_replace_callback() function, which can customize the callback function to meet your advanced requirements. Such as designing filters, etc.
preg_replace_callback
(PHP 4 >= 4.0.5, PHP 5)
preg_replace_callback -- Use callback function to perform regular expression search and replace instructions
mixed preg_replace_callback (mixed pattern, callback callback, mixed subject [, int limit])
This function behaves almost like preg_replace(), except that instead of providing a replacement parameter, a callback function is specified. This function takes as input an array of matches in the target string and returns the string used for replacement.
Example 1. preg_replace_callback() example
<?php
// This text is for 2002,
// Now want to make it available for 2003
$text = "April fools day is 04/01/2002n";
$text.= "Last christmas was 12/24/2001n";
// callback function
function next_year($matches) {
// Normally: $matches[0] is the complete match
// $matches[1] is the match for the subpattern in the first bracket
// and so on
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
"|(d{2}/d{2}/)(d{4})|",
"next_year",
$text);
//The result is:
// April fools day is 04/01/2003
// Last Christmas was 12/24/2002
?>
You'll often need the callback function for a preg_replace_callback() in just one place. In this case you can use create_function() to declare an anonymous function as callback within the call to preg_replace_callback(). By doing it this way you have all information for the call in one place and do not clutter the function namespace with a callback functions name not used anywhere else.
For friends who use the preg_replace_callback() function, you should probably need the callback function (otherwise, why use it? Isn't it better to use preg_replace directly), but it is often only used in one place. In this case you can use create_function() to declare an anonymous function as the callback function of preg_replace_callback(). In this way, we satisfy the need to declare information without getting confused by a function name that will not be used again.
Example 2. preg_replace_callback() and create_function()
<?php
/* A UNIX-style command line filter that removes the string at the beginning of each paragraph
*Convert uppercase letters to lowercase letters*/
$fp = fopen("php://stdin", "r") or die("can't read stdin");
while (!feof($fp)) {
$line = fgets($fp);
$line = preg_replace_callback(
'|<p>s*w|',
create_function(
// The use of single quotes here is critical,
// Otherwise, replace all $ with $
'$matches',
'return strtolower($matches[0]);'
),
$line
);
echo $line;
}
fclose($fp);
?>
Finally
preg_split
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
preg_split – Split a string using regular expressions. I won’t go into details here.
illustrate
array preg_split ( string pattern, string subject [, int limit [, int flags]] )
returns an array containing the substrings in subject split along the boundaries matching pattern.
If limit is specified, at most limit substrings are returned. If limit is -1, it means there is no limit and can be used to continue specifying optional parameter flags.
flags can be any combination of the following flags (combined with the bitwise OR operator | ):
PREG_SPLIT_NO_EMPTY
If this flag is set, preg_split() only returns non-empty components.
PREG_SPLIT_DELIM_CAPTURE
If this flag is set, bracket expressions in delimiter patterns are also captured and returned. This tag was added in PHP 4.0.5.
PREG_SPLIT_OFFSET_CAPTURE
If this flag is set, the associated string offset of each occurrence of the matching result will also be returned. Note that this changes the value of the returned array so that each cell is also an array, where the first item is the matched string and the second item is its offset within the subject. This tag is available since PHP 4.3.0.
Tip: If you don't need the power of regular expressions, you can choose to use faster (and simpler) alternatives such as explode() or str_split().