2012-03-11 32 views
0

我在搜索bot行爲時獲得了以下功能。當我使用此代碼時,我得到eregi()表達式的錯誤。我不是調情表達的專家。由於ereri()已折舊,我得到相同的錯誤。Eregi需要更換?

function check_if_spider() 
     { 
      // Add as many spiders you want in this array 
      $spiders = array('Googlebot', 'Yammybot', 'Openbot', 'Yahoo', 'Slurp', 'msnbot', 'ia_archiver', 'Lycos', 'Scooter', 'AltaVista', 'Teoma', 'Gigabot', 'Googlebot-Mobile'); 

      // Loop through each spider and check if it appears in 
      // the User Agent 
      foreach ($spiders as $spider) 
      { 
       if (eregi($spider, $_SERVER['HTTP_USER_AGENT'])) 
       { 
        return TRUE; 
       } 
      } 
      return FALSE; 
     } 

我該如何修改代碼才能使其工作?谷歌搜索說,它應該被轉換爲preg_match();是一個新手,我在失敗,雖然我嘗試在我的結局..可以有人指導我?

+0

[將ereg表達式轉換爲preg]的可能副本(http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) – mario 2012-03-11 23:38:54

回答

4

您實際上並沒有使用正則表達式(您正在匹配文字字符串),因此不需要使用preg_match()

更換

if (eregi($spider, $_SERVER['HTTP_USER_AGENT'])) 

if (strpos($spider, $_SERVER['HTTP_USER_AGENT']) !== FALSE) 
0

您可以使用該功能strstr比較字符串。 如果第一個參數中的字符串不包含第二個參數中的字符串,則返回false。

foreach ($spiders as $spider) 
    { 
     if(strstr($_SERVER['HTTP_USER_AGENT'], $spider)) 
     { 
      return TRUE; 
     } 
    }