PHP string comparison was first processed using regular expressions, for example, to find one or several matching characters in a string, but the complexity of regular expressions is not easy for all PHP designers. The PHP official also launched a preg_match function to solve this problem. If you need to let PHP do string matching, you only need to set the parameters of the preg_match function to let PHP automatically perform a specific string matching with the same accuracy. High, let’s take a look at the basic grammatical rules first, and then apply them to practical applications.
PHP preg_match function syntax rulesint preg_match (string pattern, string subject [, array matches [, int flags]])
This function has multiple parameters that need to be set. Starting from the first parameter, pattern is the string to be compared, and the second parameter subject is the original string. The preg_match function performs string comparison process. Whether there is a matching value in the subject string of the second comparison, if there is a matching value, it will be returned once, and there will be a matching result with the table, and then the function will stop the comparison, so if there is a matching matching result, The preg_match function will return the number 1. If there is no matching result, the function will return 0. If there is any error in the matching process, the preg_match function will return FALSE.The third parameter rray matches is used to put the comparison results in the array and select items.Use preg_match function to perform PHP string matching<?php
$subject="Today’s weather is really good";
$pattern="weather";
if (preg_match("/\weather/i", $subject)){
echo "match";
}else {
echo "not match ";
}
?>
Comparison resultmatch
In the example, we have prepared two traditional Chinese character strings. We need to use the preg_match function to compare whether there is a result that matches $pattern in the $subject string. According to the comparison result, we can see that there is a match. This is a simple PHP word For string comparison methods, the preg_match function has a more complete usage. Please refer to the detailed introduction of " PHP preg_match Normal Representation Comparison ", which includes a complete comparison setting method.
Post a Comment
0 Comments