What are the differences between using eregi and preg_grep functions in PHP for pattern matching, and when should each be used?

The main difference between eregi and preg_grep functions in PHP for pattern matching is that eregi is case-insensitive, while preg_grep is case-sensitive. eregi is a simple way to perform case-insensitive pattern matching, while preg_grep offers more flexibility and power with regular expressions. Use eregi when you need a quick and simple case-insensitive match, and use preg_grep when you need more advanced pattern matching capabilities.

// Using eregi for case-insensitive pattern matching
$string = "Hello World";
$pattern = "/hello/i";
if (eregi($pattern, $string)) {
    echo "Match found using eregi.";
} else {
    echo "No match found using eregi.";
}

// Using preg_grep for case-sensitive pattern matching
$array = ["apple", "banana", "cherry"];
$pattern = "/^b/";
$matches = preg_grep($pattern, $array);
print_r($matches);