How can the ereg() function be properly replaced with preg_match() in PHP to avoid errors like the "Unknown modifier '-' error"?
When replacing the ereg() function with preg_match() in PHP, it's important to note that the delimiter used in the regular expression pattern must be escaped to avoid errors like the "Unknown modifier '-' error." To solve this issue, you can simply escape the delimiter with a backslash (\) in the regular expression pattern.
// Original code using ereg()
$string = "Hello-world";
if (ereg("Hello-",$string)) {
echo "Match found";
}
// Updated code using preg_match() with escaped delimiter
$string = "Hello-world";
if (preg_match("/Hello-/",$string)) {
echo "Match found";
}