1

I hope someone can help!!!

In coding a form validation, I got the error message "Deprecated: Function ereg() is deprecated in E:\Zacel_Development\sa_model_watch.co.za\insert_newProf.php on line 184"

I did some research and found out I need to change !eregi and !ereg to preg_match...

I did try this, but to no avail... can anyone please check my code and advise as I am stumped!

My Code Snippet:

/* Check is numeric*/
$regex = "[0-9]{10}";
   if(!ereg($regex,$field)){
   $form->setError($fieldValue, "* Contact number invalid");
   }

SHOULD APPARENTLY BE:

/* Check is numeric*/
$regex = "[0-9]{10}";
 if(!preg_match($regex,$field)){
 $form->setError($fieldValue, "* Contact number invalid");
 }

AND:

/* Check if valid email address */
$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
     ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
     ."\.([a-z]{2,}){1}$";
    if(!eregi($regex,$field)){
      $form->setError($fieldValue, "* Email invalid");
      }

SHOULD APPARENTLY BE:

/* Check if valid email address */
$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
     ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
     ."\.([a-z]{2,}){1}$";
    if(!preg_match($regex,$field)){
      $form->setError($fieldValue, "* Email invalid");
      }

This still doesn't work... What am I doing wrong?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Celeste
  • 15
  • 1
  • 6

2 Answers2

2

You have to open and close your regex pattern with a delimiter:

So that:

$regex = "[0-9]{10}";

becomes

$regex = "/[0-9]{10}/";

If you want the pattern to be case insensitive use the i flag

$regex = "/somepattern/i";
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
0

here you are some example:

over from ereg, preg_match can be quite intimidating. to get started here is a migration tip.

<?php
if(ereg('[^0-9A-Za-z]',$test_string)) // will be true if characters arnt 0-9, A-Z or a-z.

if(preg_match('/[^0-9A-Za-z]/',$test_string)) // this is the preg_match version. the /'s are now required.
?>
Faraona
  • 1,685
  • 2
  • 22
  • 33
  • 1
    Just to avoid confusion when reading other code: The `/` is not explicitly required. You can choose every delimiter you like. The first character of the pattern is always treated as the delimiter, so if you start your pattern e.g. with `~` this one used and is required as ending symbol instead of `/`. – KingCrunch Jun 24 '11 at 09:10