You can use PHP's built-in functions. preg_match() Use this function for regular expression matching. Here is an example function: its first parameter is a regular expression, and its second parameter is the content to match:
function match_regex($pattern, $subject) {
if (preg_match($pattern, $subject, $matches)) {
return $matches;
} else {
return false;
}
}This function returns an array of matching results; if no matches are found, it returns `false`.
For example, if you want to determine whether a string is a valid email address, you can call this function as follows:
$pattern = '/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/';
$subject = 'example@example.com';
$result = match_regex($pattern, $subject);
if ($result) {
echo "匹配成功!";
} else {
echo "匹配失败!";
}In this example, the regular expression $pattern Matches the email address format; variable $subject Contains the content to match – Function match_regex() Returns an array of matching results or `false`.