0

Possible Duplicate:
How can I convert ereg expressions to preg in PHP?

I have a php contact form that I have used on a few different websites. I am trying to implement the same one on a new site, but I am getting the following message:

Deprecated: Function eregi() is deprecated in /home/content/58/10223058/html/contact-1.php on line 36

Here is line 36 from my code:

if(!eregi($string_exp,$name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }

I am sure there is an easy fix for this, but I am very new to PHP and do not know how to go about fixing this!

Any help would be greatly appreciated. Thankyou

Community
  • 1
  • 1
Matt Jacobs
  • 13
  • 1
  • 1
  • 2
  • 1
    So the proper action in your case will be to convert the variable `$string_exp` to a PCRE expression and use `preg_match()` instead of `eregi()`. – Michael Berkowski Jan 02 '13 at 18:13
  • eregi is deprecated in PHP 5.3. http://php.net/manual/en/function.eregi.php If your different websites upgrade to PHP 5.3+, you will have the same problem there. – MECU Jan 02 '13 at 18:14

2 Answers2

4

1) It's just a warning. I'm guessing the code still works, despite the warning. Correct?

2) You can fix it by Googling "Function eregi() is deprecated". For example:

http://takien.com/513/how-to-fix-function-eregi-is-deprecated-in-php-5-3-0.php

// Old
if(!eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $str)) {
    $msg = 'email is not valid';
}
else {
    $valid = true;
}

// New
if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $str)) {
    $msg = 'email is not valid';
}
else {
    $valid = true;
}

Complete PHP on-line documentation is here:

The "man page" for preg_match() is here:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
3

In most cases you can replace

if(!eregi($string_exp,$name)) {

with

if(!preg_match('/'.$string_exp.'/i',$name)) {

but not always - you have to check the pattern ($string_exp).

You could, however, just disable the message by putting this somewhere before the eregi() call:

error_reporting(error_reporting() & ~E_DEPRECATED);
AndreKR
  • 32,613
  • 18
  • 106
  • 168