Summary and analysis of php's regular processing function_PHP tutorial

WBOY
Release: 2016-07-21 15:51:39
Original
762 people have browsed it


preg_grep
(PHP 4, PHP 5)

preg_grep -- Returns the array element matching the pattern
Description
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.

flags can be the following flags:


PREG_GREP_INVERT
If passed in this flag, preg_grep() will return 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 Returns 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');

// Returns 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 -- Perform regular expression matching
Description
int preg_match ( string pattern, string subject [, array matches [, int flags]] )


Search in the subject string for content that matches the regular expression given by pattern.

If matches are provided, it will be populated with the search results. $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 tags:


PREG_OFFSET_CAPTURE
If this tag is set, the associated string offset of each matching result will also be returned. 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 check whether a string is contained in 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.

例:


if (preg_match ("/(\bweb\b)\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.";
}

?>

// 从 URL 中取得主机名
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
$host = $matches[2];

// 从主机名中取得后面两段
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 -- 进行全局正则表达式匹配

手册上该函数的解释非常明确,就不多做说明了。
说明
int preg_match_all ( string pattern, string subject, array matches [, int flags] )


在 subject 中搜索所有与 pattern 给出的正则表达式匹配的内容并将结果以 flags 指定的顺序放到 matches 中。 

搜索到第一个匹配项之后,接下来的搜索从上一个匹配项末尾开始。 

flags 可以是下列标记的组合(注意把 PREG_PATTERN_ORDER 和 PREG_SET_ORDER 合起来用没有意义): 


PREG_PATTERN_ORDER 
对结果排序使 $matches[0] 为全部模式匹配的数组,$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推。 



preg_match_all ("|<[^>]+>(.*)]+>|U",
     "example: 

this is a test
",
     $out, PREG_PATTERN_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
?>  

本例将输出: 

example: 
this is a test

example: , this is a test


因此,$out[0] 包含匹配整个模式的字符串,$out[1] 包含一对 HTML 标记之间的字符串。 



PREG_SET_ORDER 
对结果排序使 $matches[0] 为第一组匹配项的数组,$matches[1] 为第二组匹配项的数组,以此类推。 


preg_match_all ("|<[^>]+>(.*)]+>|U",
     "example: 
this is a test
",
     $out, PREG_SET_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
?>  

本例将输出: 

example: , example:
this is a test
, this is a test




In this example, $matches[0] is the first set of matching results, $matches[0][0] contains the results that match the entire pattern Text, $matches[0][1] contains the text matching the first subpattern, 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 of each matching result will also be returned. 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 within 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 is matched (possibly zero), or FALSE if an error occurs.

Example 1. Get all phone numbers from a certain text

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)

// \2 is an example of a reverse reference, its meaning in PCRE Yes
// must match the content within the second set of brackets in the regular expression itself. In this example,
// is ([w]+). Because the string is in double quotes, you need
// to add an extra backslash.
$html = "bold textclick me";

preg_match_all ("/(<([w ]+)[^>]*>)(.*)()/", $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 -- Escape regular expression character
Description
string preg_quote ( string str [, string delimiter] )


preg_quote() takes str as parameter and Prepend each character that is part of the regular expression syntax with a backslash. 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 parameter delimiter is provided, this character will also be escaped. Can be used to escape the delimiter required by the PCRE function. The most commonly used delimiter is the slash /.

Special characters of regular expressions include: . + * ? [ ^ ] $ ( ) { } = ! < > | :.

Note: This function can be safely used on binary objects.

The above is the explanation in the manual, which is very clear and 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

$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords , '/');
echo $keywords; // returns $40 for a g3/400
?>



Example 2. Give a word in a certain text Add italic tags

// In this example, preg_quote($word) is used to make the asterisks not in the regular expression
// have special meaning.

$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/".preg_quote($word )."/",
"".$word."",
$textbody);
?>



The next step is to apply the super flexible, super powerful, and widely used preg_replace function.

preg_replace
(PHP 3 >= 3.0.9, PHP 4, PHP 5)

preg_replace -- Perform regular expression search and replacement
Instructions
mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit] )


Search for a match of pattern pattern in subject and replace it 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 backreferences in the form \n or (since PHP 4.0.4) in the form $n , with the latter 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 it unclear whether preg_replace() 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 pattern and replacement are both 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 you use an index to identify which pattern will be replaced by which replacement, you should use ksort() to sort the array 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 values ​​from them in order to search and replace subject. If there are fewer values ​​in replacement than in pattern, the 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 backreference replacement). 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 replacement
Description
mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )


The behavior of this function is almost the same as 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

// This text is for 2002,
// Now I want to make it available for 2003 Year
$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 first sub-pattern in brackets
/ / 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? It is not 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()

/* A UNIX-style command line filter that removes the
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(
'|

s*w|',
create_function(
/ /Here is the key to using a single quotation number,
// Otherwise, you will replace all $ to $
'$ matches',
' Return StrTolower ($ matches [0]);
$line
);
echo $line;
}
fclose($fp);
?>


Finally

preg_split
(PHP 3 >= 3.0.9, PHP 4, PHP 5)

preg_split -- Use regular expressions to split strings
No more details.
Description
array preg_split ( string pattern, string subject [, int limit [, int flags]] )


Returns an array containing the edges in subject along the boundaries that match pattern Split substring.

If limit is specified, at most limit substring will be returned. If limit is -1, it means there is no limit and can be used to continue to specify 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-null components.

PREG_SPLIT_DELIM_CAPTURE
If this flag is set, bracket expressions in the delimiter pattern will also be 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 matching result will also be returned. 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 within subject . This tag is available since PHP 4.3.0.


Tip: If you don’t need the functionality of regular expressions, you can choose to use faster (and simpler) alternative functions such as explode() or str_split().

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319064.htmlTechArticlepreg_grep (PHP4,PHP5) preg_grep--returns the array unit description that matches the pattern arraypreg_grep(stringpattern,arrayinput[, intflags]) preg_grep() returns an array, including inpu...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!