What are the differences between ereg and preg functions in PHP, and when should each be used?
The main difference between ereg and preg functions in PHP is that ereg is a deprecated regular expression function that is case-insensitive by default, while preg is the preferred regular expression function that allows for more advanced features and is case-sensitive by default. It is recommended to use preg functions for new development as ereg functions are no longer supported in PHP 7 and above.
// Using preg function to match a pattern in a string
$string = "Hello, World!";
if (preg_match("/hello/i", $string)) {
echo "Pattern found in string.";
} else {
echo "Pattern not found in string.";
}