What are the potential pitfalls of using preg_match_all instead of preg_match to check if a text matches a regex pattern in PHP?
Using preg_match_all instead of preg_match can lead to unexpected results because preg_match_all returns all matches of the pattern in the text, while preg_match only returns the first match. To check if a text matches a regex pattern using preg_match_all, you should check if the count of matches is greater than 0.
$text = "Hello, World!";
$pattern = "/Hello/";
$matches = [];
preg_match_all($pattern, $text, $matches);
if (count($matches[0]) > 0) {
echo "Text matches the pattern.";
} else {
echo "Text does not match the pattern.";
}