[英]Eregi() Deprecated php help?
我收到錯誤:
Deprecated: Function eregi() is deprecated in C:\wamp\www\registration\class.register.php on line 75
用我的代碼::
if(empty($this->email) || !eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$',$this->email))
我應該使用哪種替代方法,以及如何實施呢?
正如@Sarfraz所說,不建議使用ereg_*
函數,而應使用preg_*
。 但是,在這種情況下,您根本不應使用正則表達式。 有一個名為filter_var()
函數,該函數使您可以驗證一些流行的數據格式(電子郵件,URL等)。
if (empty($this->email) || false == filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
// Empty or not valid email
}
是的, 不建議使用 ereg
系列功能,您需要改用preg
系列功能。 在您的情況下,應改用preg_match
。
該段代碼等效於:
if(empty($this->email) ||
!preg_match('~^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$~i',
$this->email))
它也可以壓縮為:
if(empty($this->email) || !preg_match('~^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,4}$~i',
$this->email))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.