0

Hi I have the following code that that uses the eregi() function that PHP is saying is depreciated. I am trying to convert it into a replacement function (preg match) but im not too sure how to go about it as I am new to preg match function.

eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email'])))

How can I layout the above code in the preg match function?

Thanks

M9A
  • 3,168
  • 14
  • 51
  • 79
  • 4
    just another one of thousands of questions of this kind... Replace `[:alnum:]` with `[0-9A-Za-z]` – Alex Shesterov Jul 10 '13 at 11:20
  • 1
    Why one would not use a framework for validation but a random, most likely incomplete regular expression? – Michal M. Jul 10 '13 at 11:22
  • 2
    It looks very much as if that regex is trying to match an email address. Please note that there are valid addresses in existence that will fail this regex. Please note that PHP provides the [`filter_var()`](http://php.net/manual/en/function.filter-var.php) function which is designed to validate email addresses without the need for a regex. I strongly recommend you use this instead. – Spudley Jul 10 '13 at 11:26
  • also, there are dozens of duplicates of this question already -- I've flagged one of them, but there are plenty more. Please do a bit of basic searching before asking a question. – Spudley Jul 10 '13 at 11:30

1 Answers1

1
filter_var(stripslashes(trim($_POST['email'])), FILTER_VALIDATE_EMAIL) 

will do the task you are trying to complete, and it validates email addresses even better ;)

Otherwise use preg_match and replace [:alnum:] with [0-9A-Za-z] in the pattern.

See

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103