Search code examples
phpregexereg

ereg function in php


When I write this code :

$pat='^[A-Za-z][a-zA-Z0-9_\-\.]*@[a-zA-z0-9\-]+\.[a-zA-Z0-9\-\.]+$';
$mail='javad.y1';
ereg($pat,$mail);

I'm getting this error :

Deprecated: Function ereg() is deprecated in C:\wamp\www\Test\test.php on line 10


Solution

  • The statement "Error : Deprecated: Function ereg() is deprecated" pretty much answers the question for you.

    In terms of using the more modern equivalent, see the Differences from POSIX regex page in the PHP manual and the preg_match function you'll need to use going forward.

    Alternatively, for some exciting further reading why not check out: http://en.wikipedia.org/wiki/Deprecated

    UPDATED WITH SAMPLE CODE

    If you're attempting to validate an email, then you could use:

    if(preg_match("/^[A-Za-z][a-zA-Z0-9_\-\.]*@[a-zA-z0-9\-]+\.[a-zA-Z0-9\-\.]+/", $email)) {
        // The email is valid. Yay for stuff! And things!
    }
    

    That said, I wouldn't say this is necessarily the best approach.