Regular expressions are a very important point of knowledge in PHP, often used to find and replace strings, to verify that the information format that the user has entered conforms to specifications, such as message format, phone format, and so on. There are also collectors and the like in the software, the regular is also necessary! It mainly introduces several common regular functions in PHP: Preg_match (), Preg_match_all (), Preg_grep (), Preg_split ().
1.preg_match ()
This function is commonly used for form validation. You can search and match strings in the specified regular expression pattern. The function has two required parameters, the first parameter needs to provide a pattern written by the user in regular expression syntax, and the second parameter requires a string. If you provide a third optional array parameter, matches, you can have a matching result that holds the parts of the sub-pattern in the first argument.
2.preg_match_all ()
The function is similar to the Preg_match () function, unlike the function Preg_match () The search stops after the first match. The function Preg_match_all () will always search to the end of the specified string, and can get all matching results.
This function puts all possible matching results into the array of the third parameter, returns the number of times the pattern matches, and returns False if an error occurs. If the fourth argument is used, the matching result of each occurrence is saved to the array of the third parameter, according to the order in which it is specified. The fourth parameter has two predefined values,
A.preg_pattern_order: It is the default value of the Preg_match_all () function, the result is sorted so that $matches[0] is an array of all pattern matches, $matches [1] An array of strings that match the sub-patterns in the first parenthesis, and so on.
B.preg_set_order: The result sort is $matches[0] is an array of the first set of matches, $matches [1] is an array of the second set of occurrences, and so on.
3.preg_grep ()
Unlike the first two functions, the function matches the elements in the array and returns the array cells that match the regular expression.
The function returns an array that contains the cells in the second parameter array that match the given first parameter pattern, which is vaguely matched for each element of the output array.
4.preg_replace ()
The function performs the search and substitution of regular expressions, and is one of the most powerful string substitution handler functions.
The function has three parameters, the first parameter matches, and two parameters are replaced. The third parameter to be searched more than one. If the fourth optional parameter limit is specified, only the limit match is replaced, and if the limit is given or the value is-1, all occurrences are replaced.
5.str_replace ()
The function is a PHP-enhanced string-handling function, or it can implement a replacement wage for a string. Although the substitution function with no regular expression is powerful, the substitution of some simple strings is more efficient than the preg_replace () function.
The function has a single required parameter, an optional parameter, the first parameter is the target object, the second parameter is the replacement object, the third is the processed string, the function in the string of the third argument, the case-sensitive way to search for the first parameter to improve the target object, Replaces all instances found with the replacement object provided by the second parameter. If the target object is not searched in the third argument, the processed string remains unchanged. The fourth is an optional argument, a reference to a variable that must pass in a variable name to save the number of substitutions. The first two parameters of the
using Str_replace () can not only use a string, but also make an array.
6.preg_split ()
The function uses a Perl-compatible regular expression syntax that can be used to split strings by regular expression methods. The
function returns a string array in which the element contains a string through the second argument, a regular expression of the first parameter, as a substring of the matched boundary segment. If a third string of limit is specified, a maximum of linmit substrings are returned. And the last element in it contains all the parts that are left after being split. If limit is-1, it means there is no limit. The fourth parameter is an optional parameter,
>> 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
>> preg_split_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 in it is also an array, where the first item is the matching string, and the second item is its offset in the original string.
function explode ()
If you are splitting with only a particular string, we recommend using the explode () function, which does not have to call the regular expression engine, so the speed is the fastest.
The function has three parameters, the first parameter increases a split character or string, the second argument is a split string, and if the third optional parameter limit is provided, specifies the maximum number of substrings to be split into. The function returns an array of separated substrings.
function implode ()
corresponding to the split string is the implode () function, which combines all the elements in the array into a single string. The function join () is the alias of the function.
The function has two parameters, the first parameter raises a link character or string, and the second parameter specifies a linked array.
Attached: What is a regular expression?
Tags: PHP programming
Source: The use of regular expressions in PHP
The use of regular expressions in PHP