문제 설명
검색으로 배열에서 특정 데이터 가져오기 (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가 그것을 만들 수 있지만 방법을 모르고 구문을 이해하지 못합니다.
참조 솔루션
방법 1:
If you get an array something like below then Try PHP strpos function
$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
}
}
(by Evolution48、Basheer Kharoti)