簡體   English   中英

通過搜索從數組中獲取特定數據

[英]get specific datas from a array by a search

我有一個包含許多不同數據的數組,我從服務器獲取數據:

 $sp_bots = shell_exec("grep bot | awk '{print $12}' /var/www/laravel/logs/vhosts/website");

在此注釋中,我獲得了“機器人”一詞所在的所有數據-然后我通過awk刪除了某些內容

Mozilla/5.0 [82] => "Mozilla/5.0 [83] => "Googlebot-Image/1.0" [84] => "Googlebot-Image/1.0" [85]

這就是我現在得到的

但是我只想要找到“機器人”的數據! 我只是不知道如何.. array_filter可以做到,但我不知道如何並且不了解語法。

如果您得到的數組如下所示,請嘗試使用PHP strpos函數

$sp_bots= array( 82 => "Mozilla/5.0", 83 => "Googlebot-Image/1.0", 84 => "Googlebot-Image/1.0" );
foreach($sp_bots as $data) {
    if(strpos($data, 'bot' ) > -1 ) {
      // It does contain bot string
      echo $data .' contains bot string <br/>';//while it will print the other two indexes
    } else {
      // It does not contain bot value
      echo $data .' does not contains bot string <br/>'; //Mozilla/5.0 does not contain bot string
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM