繁体   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