..
The preg_match function belongs to the family of PCRE, where the acronym stands for PCRE Perl Compatible Regular Expression.
This function is used to make comparisons on the basis of models represented in the form of regular expressions.
The preg_match accepts 5 parameters, the first two are mandatory and are, in order, the pattern and the string to be searched. The third parameter, optional, is the definition of an array of results that will hold the occurrences found. Fourth and fifth parameters are, respectively, flags and offset: both are optional to have utility only in special cases, for which I refer to the official PHP documentation.
The preg_match returns 0 if there is no recognition, and 1 if recognition occurred (this differs from preg_match preg_match_all : it does not stop at the first recognition but continues until the end of subject). If an error preg_match () will return FALSE.
We see a basic example that uses only the first two parameters:
<? Php
if (preg_match ('/ mrwebmaster / i', 'MrWebmaster is my favorite site!')) {
echo "A match was found."
Else {}
echo "No recognition."
}
?>
If we wanted to extract the result would have been inserted as a third parameter, we recall, is an array:
<? Php
if (preg_match ('/ mrwebmaster / i', 'MrWebmaster is my favorite site!', $ result)) {
print_r ($ result);
}
?>
| |
Linux (Course)
Complete guide to open-source system. From 49 €. |
| |
MySQL (Course)
Management of open-source database. From 39 €. |
| |
PHP (Course)
Full course for creating dynamic Web sites. From 49 €. |