-3

This is what I get when I try out the contact form on my website: Deprecated: Function eregi() is deprecated in /home/content/04/11965204/html/horizon/One/contact.php on line 9 Message sent!

Here is my code: Please help - thanks

<?php

if(!$_POST) exit;

$email = $_POST['email'];

//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST

['email']) ? '' : 'INVALID EMAIL ADDRESS';

if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]
{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}

if($errors==1) echo $error;
else{
    $values = array ('name','email','message');
    $required = array('name','email','message');
    $your_email = "horizon.electronics.recyclers@gmail.com";
    $email_subject = "New Message: ".$_POST['subject'];
    $email_content = "new message:\n";

    foreach($values as $key => $value){
        if(in_array($value,$required)){
            if ($key != 'subject' && $key != 'company') {
                if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
            }
            $email_content .= $value.': '.$_POST[$value]."\n";
        }
    }

    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Message sent!'; 
    } else {
        echo 'ERROR!';
    }
}

?>
gronostaj
  • 2,231
  • 2
  • 23
  • 43
  • 5
    Use `preg_match()` instead. Which has been commented out in your code for some reason or another. – Funk Forty Niner Dec 28 '13 at 21:48
  • I like how the PHP manual says `eregi()` is deprecated _but doesn't say what the replacement is_ – Bojangles Dec 28 '13 at 21:51
  • possible duplicate of [How to change PHP's eregi to preg\_match](http://stackoverflow.com/questions/1374881/how-to-change-phps-eregi-to-preg-match) – mario Dec 28 '13 at 21:53
  • 1
    http://www.php.net/manual/en/function.eregi.php => *"Tip eregi() is deprecated as of PHP 5.3.0. `preg_match()` with the i (PCRE_CASELESS) modifier is the suggested alternative."* @Bojangles – Funk Forty Niner Dec 28 '13 at 21:53
  • It's there, but it's half a screen below the fold, and it's a tip. Not even a warning. Why isn't the Big Red Box saying what the error is? Typical PHP – Bojangles Dec 29 '13 at 15:50

3 Answers3

2

In PHP 5.3+, ereg and eregi functions are deprecated. To replace

eregi('pattern', $string, $matches) 

use

preg_match('/pattern/i', $string, $matches)

(the trailing i in the first argument means ignorecase and corresponds to the i in eregi - just skip in case of replacing ereg call).

But be aware of differences between the new and old patterns! This page lists the main differences, but for more complicated regular expressions you have to look in more detail at the differences between POSIX regex (supported by the old ereg/eregi/split functions etc.) and the PCRE.

However in your case, there's no need to worry and just do the replacement sketched above.

Tomas
  • 57,621
  • 49
  • 238
  • 373
  • @user3142811 If this answers your question, just tick the mark to the left of the post to mark it as the accepted answer. – Tomas Dec 28 '13 at 22:03
1

Deprecation is a status applied to a computer software feature, characteristic, or practice indicating it should be avoided, typically because of it being superseded.

eregi() is deprecated as of PHP 5.3, use preg_match() instead.

sergio
  • 5,210
  • 7
  • 24
  • 46
0

Instead of using the deprecated eregi() function or preg_match() to validate an email address, you're best to use FILTER_VALIDATE_EMAIL and it uses a lot LESS code.

Example and an alternative:

$email = $_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Thank you. Your email is valid.";
  // The email address is valid
} else {

echo "INVALID EMAIL ADDRESS";
  // The email address is not valid
}

Or by using an inverted method: !filter_var

if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
  echo "INVALID EMAIL ADDRESS";
  }
else
  {
  echo "Your email is valid.";
  }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141