2010-08-22 48 views
2

我recieving錯誤:Eregi()不推薦使用PHP幫助?

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._-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$',$this->email)) 

我應該使用什麼樣的替代,我怎麼能實現它????

回答

2

ereg家庭功能have been deprecated,您需要使用preg家庭功能代替。在你的情況下,你應該使用preg_match

這一段代碼就相當於:

if(empty($this->email) || 
    !preg_match('~^[a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$~i', 
    $this->email)) 

它也可以壓縮到:

if(empty($this->email) || !preg_match('~^[\w.-][email protected][\w.-]+\.[a-zA-Z]{2,4}$~i', 
    $this->email)) 
3

正如@Sarfraz說ereg_*功能已被取消,你應該使用preg_*代替。但是在這種情況下,你根本不應該使用正則表達式。有一個叫filter_var()的功能,允許你驗證一些流行的數據格式(電子郵件,URL等)

if (empty($this->email) || false == filter_var($this->email, FILTER_VALIDATE_EMAIL)) { 
    // Empty or not valid email 
} 
+0

notabede email是一個URL。 ;) – Crozin 2010-08-22 18:00:03