Preg_grep
(PHP 4, PHP 5)
Preg_grep--Returns the array unit that matches the pattern
Description
Array Preg_grep (string pattern, array input [, int flags])
Preg_grep () returns an array that includes cells in the input array that match the given pattern pattern.
Flags can be the following tags:
Preg_grep_invert
If this token is passed in, Preg_grep () returns the cells in the input array that do not match the given pattern. This tag is available from PHP 4.2.0.
Starting with PHP 4.0.4, the results returned by PREG_GREP () are indexed using the key names from the input array. If you do not want such a result, use Array_values () to re-index the result returned by Preg_grep ().
Above is a description of Preg_grep () on the manual. First, this is a Perl-compatible regular function, so I guess Preg_grep means P (Perl) reg (Regular) _grep, which is characterized by the use of arrays, which can be used to do regular matches in multidimensional arrays, You can also return a matching or non-matching array through the flags parameter. It is more efficient than using foreach (...). {if ...} The structure is much faster (unverified) and can match complex patterns. It is used in search, sorting and other applications.
Cases:
$arr = Array (' abc ' =>12.213, ' BB ' =>12345, ' ba ' =>23.2321,34.3, ' the ' 3.3 ', ' 23434 ' = ' bbb ');
Returns all array elements that contain 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]])
Searches the subject string for content that matches the regular expression given by pattern.
If matches is provided, it is populated with the results of the search. $matches [0] will contain text that matches the entire pattern, $matches [1] will contain the text that matches the sub-pattern in the first captured parenthesis, and so on.
Flags can be the following tags:
Preg_offset_capture
If this tag is set, the matching result for each occurrence also returns its subordinate string offset. Note that this changes the value of the returned array so that each cell is also an array, where the first item is the matching string, and the second item is its offset. This tag is available from PHP 4.3.0.
The flags parameter is available from PHP 4.3.0.
Preg_match () returns the number of times that pattern matches. Either 0 times (no match) or 1 times, because Preg_match () stops searching after the first match. Preg_match_all () Instead, it searches until the end of subject. If error Preg_match () returns FALSE.
Tip: If you only want to see if a string is contained in another string, do not use Preg_match (). Can be replaced with strpos () or strstr (), much faster.
The above is a description of Preg_match () in the manual, and I think the function is that he can do the validation, that is, whether a string conforms to a particular requirement. The limitation is that the above is either matched 0 times or 1 times. And the return value is the number of matches. Preg_match_all () can be used when a full match is required. It is also worth mentioning that the function of the $matches array can be done from the return value of the pattern, which is sometimes useful.
Cases:
if (Preg_match ("/(\bweb\b) \s (\d)/I", "PHP is the Web Scripting Web language of choice.", $match)) {
Print "A match was found.";
Print_r ($match);
} else {
Print "A match is not found.";
}
?>
Get host name from URL
Preg_match ("/^ (http:\/\/)?" ( [^\/]+)/I ",
"Http://www.php.net/index.html", $matches);
$host = $matches [2];
Get the next two paragraphs from the hostname
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--Global regular expression matching
The explanation of the function in the manual is very clear, so there is no more explanation.
Description
int Preg_match_all (string pattern, string subject, array matches [, int flags])
Searches in subject for all content that matches the regular expression given by the pattern and places the result in matches in the order specified by flags.
After the first match is searched, the next search starts at the end of the previous match.
Flags can be a combination of the following tags (note that it makes no sense to combine Preg_pattern_order and Preg_set_order):
Preg_pattern_order
The results are sorted so that the $matches [0] is an array of all pattern matches, $matches [1] is an array of strings that match the sub-patterns in the first parenthesis, and so on.
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 ";
?>
This example will output:
Example:, this is a test
Example:, this is a test
Therefore, $out [0] contains a string that matches the entire pattern, $out [1] contains a string between a pair of HTML tags.
Preg_set_order
The result is sorted so that $matches [0] is an array of the first set of matches, $matches [1] is an array of the second set of matches, and so on.
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 ";
?>
This example will output:
Example:, Example:
This is a test that is a test
In this example, $matches [0] is the first set of matching results, $matches [0][0] contains text that matches the entire pattern, $matches [0][1] contains the text that matches the first sub-pattern, and so on. Similarly, $matches [1] is the second set of matching results, and so on.
Preg_offset_capture
If this tag is set, the matching result for each occurrence also returns its subordinate string offset. Note that this changes the value of the returned array so that each cell is also an array, where the first item is the matching string, and the second item is its offset in subject. This tag is available from PHP 4.3.0.
If no token is given, it is assumed to be preg_pattern_order.
Returns the number of times the entire pattern match (possibly 0), if an error returns FALSE.
Example 1. Get all the phone numbers from a 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, and its meaning in PCRE is
Must match the contents of the second set of parentheses in the regular expression itself, in this case
Is ([\w]+). Because the strings are in double quotes, you need to
Add a backslash.
$html = "
Bold textClick Me ";
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]. " \ n ";
}
?>
Preg_quote
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
Preg_quote--Escaping regular expression characters
Description
String Preg_quote (String str [, String delimiter])
Preg_quote () takes the str parameter and adds a backslash to 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 an optional parameter delimiter is provided, the character is also escaped. Can be used to escape the delimiter required by the PCRE function, the most commonly used delimiter is a slash/.
Special characters for regular expressions include:. \ + * ? [ ^ ] $ ( ) { } = ! < > | :。
Note: This function can be used safely with binary objects.
The above is the explanation of the manual, but also very clear, not much to say, another note on the manual is that the function is safe for binary objects, which is useful.
Example: Example 1. Preg_quote () example
$keywords = ' $ g3/400 for a ';
$keywords = Preg_quote ($keywords, '/');
Echo $keywords; Returns \$40 for a g3\/400
?>
Example 2. Add italic markers to a word in a text
In this example, Preg_quote ($word) is used to make the asterisk not in the regular expression
Have a special meaning.
$textbody = "This book was *very* difficult to find";
$word = "*very*";
$textbody = Preg_replace ("/". Preg_quote ($word). " /",
"
". $word."",
$textbody);
?>
The next step is to apply ultra-flexible, ultra-powerful, ultra-wide preg_replace functions.
Preg_replace
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
Preg_replace--Perform search and replace of regular expressions
Description
Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Searches the subject for a match in pattern mode and replaces it with replacement. If limit is specified, only the limit match is replaced, and if the limit is omitted or the value is-1, all occurrences are replaced.
Replacement can contain a \\n form or a reverse reference (since PHP 4.0.4) $n form, preferably using the latter. Each of these references will be replaced with text that matches the sub-pattern in the nth captured parentheses. n can be from 0 to 99, where \\0 or $ refers to text that is matched by the entire pattern. The left parenthesis is counted from left to right (starting at 1) to get the number of sub-patterns.
When the replacement pattern is followed by a number immediately after a reverse reference (that is, a number immediately following a matching pattern), you cannot use a familiar \\1 symbol to represent a reverse reference. \\11, for example, will make preg_replace () unclear if it wants a \\1 inverse reference followed by a number 1 or a \\11 inverse reference. The workaround in this example is to use \${1}1. This creates an isolated reverse reference, and makes the other 1 simple text.
If a match is found, the replaced subject is returned, otherwise the original unchanged subject is returned.
Each parameter (except limit) of Preg_replace () can be an array. If both pattern and replacement are arrays, they are processed in the order in which their key names appear in the array. This is not necessarily the same as the numeric order of the index. If you use an index to identify which pattern will be replaced by which replacement, you should sort the array with Ksort () before calling Preg_replace ().
If subject is an array, the search and replace is performed on each item in the subject, and a collection is returned.
If both pattern and replacement are arrays, then preg_replace () extracts the values from each to search for and replace the subject. If the value in replacement is less 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. In turn, it doesn't make sense.
The/E modifier causes preg_replace () to treat the replacement parameter as PHP code (after the appropriate reverse reference is replaced). Tip: To make sure that replacement constitutes a valid PHP code string, PHP will have a parsing error in the report line that contains Preg_replace ().
Note: The limit parameter was added after PHP 4.0.1pl2.
I think the big thing is that he can not only deal with strings, but also handle arrays, and his inverse reference function is very flexible. Basically he can meet most of the needs of ordinary users, if he is not competent, then we also have the Preg_replace_callback () function, you can customize the callback function to meet your advanced requirements. such as design filters.
Preg_replace_callback
(PHP 4 >= 4.0.5, PHP 5)
Preg_replace_callback--Perform a search and replace of regular expressions with callback functions
Description
Mixed Preg_replace_callback (mixed pattern, callback callback, mixed subject [, int limit])
This function behaves almost like preg_replace (), except that it does not provide a replacement parameter, but instead specifies a callback function. The function takes the matching array in the target string as an input parameter and returns a string for substitution.
Example 1. Preg_replace_callback () example
This text is for 2002 years,
Now want to make it available for 2003
$text = "April fools day is 04/01/2002\n";
$text. = "Last Christmas is 12/24/2001\n";
callback function
function Next_year ($matches) {
Usually: $matches [0] is a complete match
$matches [1] is a match for the child pattern in the first parenthesis
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 a anonymous function as callback within the call to Preg_replace_ca Llback (). By doing it this is the one that has all information for the "in" place and does not clutter the function namespace with a C Allback functions name not used anywhere else.
For a friend who uses the Preg_replace_callback () function, you need to return to the callback function (otherwise, it is not better to use preg_replace), but it is often used only in one place. In this case you can use Create_function () to declare an anonymous function as the callback function of the Preg_replace_callback (). In this way, we satisfy the need to declare information, and there is no confusion about the function name that will not be used again.
Example 2. Preg_replace_callback () and Create_function ()
/* A UNIX-style command-line filter that starts each paragraph
* Uppercase letters converted to lowercase * *
$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 (
//Use single quotes here is critical,
//Otherwise replace all $ \$
' $matches ',
' return Strtolower ($ma Tches[0]); '
),
$line
);
Echo $line;
}
Fclose ($FP);
?>
Last
Preg_split
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
Preg_split--split string with regular expression
No Let me repeat.
Description
Array preg_split (string pattern, string subject [, int limit [, int flags]])
Returns an array that contains the subject A substring that is split along the boundary that matches the pattern.
If limit is specified, it returns up to the limit substring, and if limit is-1, it means there is no limit and can be used to continue specifying the optional parameter, flags.
Flags can be any combination of the following tags (in bitwise OR operator | combination):
Preg_split_no_empty
If this tag is set, Preg_split () returns only non-empty components.
Preg_split_delim_capture
If this tag is set, the parentheses expression in the delimiter pattern is also captured and returned. This tag is added to PHP 4.0.5.
Preg_split_offset_capture
If this tag is set, if this tag is set, the matching result for each occurrence also returns its subordinate string offset. Note that this changes the value of the returned array so that each cell is also an array, where the first item is the matching string, and the second item is its offset in subject. This tag is available from PHP 4.3.0.
Hint: If you do not need the functionality of a regular expression, you can choose to use a faster (and simpler) alternative function such as explode () or str_split ().
http://www.bkjia.com/PHPjc/319064.html www.bkjia.com true http://www.bkjia.com/PHPjc/319064.html techarticle Preg_grep (PHP4,PHP5) preg_grep--returns the array unit description that matches the pattern Arraypreg_grep (Stringpattern,arrayinput[,intflags]) Preg_grep ( ) returns an array that includes the Inpu ...