What is the common error message encountered when using preg_match() in PHP?
When using preg_match() in PHP, a common error message encountered is "Warning: preg_match(): No ending delimiter '/' found." This error occurs when the regular expression pattern is not properly enclosed within delimiters, such as slashes (/). To fix this issue, make sure to enclose the regular expression pattern within delimiters.
// Incorrect usage of preg_match without delimiters
$pattern = 'hello';
$string = 'hello world';
if (preg_match($pattern, $string)) {
echo 'Match found!';
} else {
echo 'No match found.';
}
// Corrected code with delimiters
$pattern = '/hello/';
$string = 'hello world';
if (preg_match($pattern, $string)) {
echo 'Match found!';
} else {
echo 'No match found.';
}