问题
I want to get the browser details of the client. so that am using $_SERVER['HTTP_USER_AGENT'] to get the details but it get some extra information also like
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
in chrome browser. what i have done is convert the string into an array like
Array ( [0] => Mozilla/5.0 [1] => (Windows [2] => NT [3] => 10.0; [4] => Win64; [5] => x64) [6] => AppleWebKit/537.36 [7] => (KHTML, [8] => like [9] => Gecko) [10] => Chrome/60.0.3112.90 [11] => Safari/537.36 )
If i search for Chr -- i want it to search the array and return Chrome/60.0.3112.90 .
Please suggest solution for this thanks.
Code :
echo $browser = $_SERVER['HTTP_USER_AGENT'];
$strArray = explode(' ',$browser);
print_r($strArray);
回答1:
You can create function to search from array
echo $browser = $_SERVER['HTTP_USER_AGENT'];
$strArray = explode(' ',$browser);
print_r($strArray);
function search_from_array($array,$string)
{
$return_str = "";
foreach ($array as $key => $value) {
$pos = stripos($value, $string);
if($pos !== false)
$return_str=$value;
}
return $return_str;
}
echo search_from_array($strArray,"chr");
回答2:
The way you're converting the user-agent to an array is quite faulty, for example "Windows NT" becomes ['Windows','NT'] and this is not something you want.
You might want to use ua-parser to extract the user-agent information in a better way.
require_once 'vendor/autoload.php';
use UAParser\Parser;
$ua = "Mozilla/5.0 (Macintosh; Intel Ma...";
$parser = Parser::create();
$result = $parser->parse($ua);
print $result->ua->family; // Safari
print $result->ua->major; // 6
print $result->ua->minor; // 0
print $result->ua->patch; // 2
print $result->ua->toString(); // Safari 6.0.2
print $result->ua->toVersion(); // 6.0.2
print $result->os->family; // Mac OS X
print $result->os->major; // 10
print $result->os->minor; // 7
print $result->os->patch; // 5
print $result->os->patchMinor; // [null]
print $result->os->toString(); // Mac OS X 10.7.5
print $result->os->toVersion(); // 10.7.5
print $result->device->family; // Other
print $result->toString(); // Safari 6.0.2/Mac OS X 10.7.5
print $result->originalUserAgent; // Mozilla/5.0 (Macintosh; Intel Ma...
回答3:
With the added need for searching for OS that is written in comments the accepted answer will not work well.
@Nabils answer will work quite well but since it splits the string in very small pieces it may be hard to use.
I thought I could use preg_split and create a good array to search and I think I made it.
I don't know all variations of user agents but they seem to follow a pattern.
This will split on space but also on (
and )
.
$input_line = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36";
$arr = preg_split("/( \()|(\) )/", $input_line);
$arr2 =explode(" ", end($arr)); // explode "Chrome/60.0.3112.90 Safari/537.36" on space
Unset($arr[count($arr)-1]); // remove above exploded
$arr = Array_merge($arr,$arr2); // reinsert them as two items
//Var_dump($arr);
$search = "Chr";
Foreach($arr as $val){
If($search == Substr($val,0,3)) echo $val;
}
See here how it works: https://3v4l.org/qF65g
回答4:
You can use regex on the string.
$str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36";
$search = "Chr";
Preg_match("/". $search . ".*?\/\d+\.\d+\.\d+\.\d+/", $str, $match);
Var_dump($match);
It will capture the search and any characters til / then your digits with dots.
https://3v4l.org/uYpoP
来源:https://stackoverflow.com/questions/45499717/how-to-search-array-with-first-3-character-of-its-value-in-php