Current Location: Home> Latest Articles> How to Resolve PHP Deprecated Warning: eregi() Function is Deprecated

How to Resolve PHP Deprecated Warning: eregi() Function is Deprecated

M66 2025-06-29

PHP Deprecated: Function eregi() is deprecated - Solution

As PHP versions evolve, some older functions are gradually deprecated, and the eregi() function is one of them. When using this function, PHP will throw a warning indicating that it may cause issues in future versions. This article will explain how to resolve the PHP deprecated warning and replace the eregi() function.

Introduction to the eregi() Function

The eregi() function was used for performing case-insensitive regular expression matching. However, it was deprecated in PHP 5.3.0 and removed entirely in PHP 7.0.0. As a result, if your code uses eregi(), you will receive a warning in PHP 5.3.0 or later versions like the following:

"PHP Deprecated: Function eregi() is deprecated in your_script.php on line X"

Solution One: Use preg_match() Instead of eregi()

preg_match() is a powerful regular expression matching function that not only supports case sensitivity but is also more efficient and standardized. Below is an example of how to replace eregi() with preg_match():

if (preg_match("/pattern/i", $string)) {

In the above code, /pattern/i is the pattern you want to match, with the 'i' flag indicating a case-insensitive match. If the match is successful, you can execute actions within the // do something part.

Solution Two: Use stripos() Instead of eregi()

If you do not need regular expression matching, you can use the stripos() function to replace eregi(). stripos() searches for a substring within a string without being case-sensitive. Below is an example of how to use stripos():

if (stripos($string, "pattern") !== false) {

In this code, "pattern" is the substring you want to find. If a match is found, you can perform the corresponding action in the // do something section.

Conclusion

By using the two solutions outlined above, you can easily replace the deprecated eregi() function and avoid the PHP deprecated warning. Depending on your needs, you can either use preg_match() for regular expression matching or stripos() for simple substring searching. Both methods effectively eliminate the warning and ensure that your code works properly in newer PHP versions.