开发者

ereg function in php [duplicate]

开发者 https://www.devze.com 2023-02-20 02:04 出处:网络
This question already has answers here: How can I convert ereg expressions to preg in PHP? (4 answer开发者_运维技巧s)
This question already has answers here: How can I convert ereg expressions to preg in PHP? (4 answer开发者_运维技巧s) Closed 3 years ago.

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


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.


Because ereg() is deprecated. You should use preg_match() instead.


You now youe Perl-compatible RegEx instead.

POSIX RegEx:

As of PHP 5.3.0 this extension is deprecated,  calling any function

provided by this extension will issue an E_DEPRECATED notice.

0

精彩评论

暂无评论...
验证码 换一张
取 消