moose-en-a-gant Posted January 12, 2015 Share Posted January 12, 2015 I'm trying to do a string match of a "what is my browser" return I counted the indexes, there are 30 I have this code, I tested four different browsers, Internet Explorer, Chrome, Chromium, Firefox Only chromium works, why? I counted the indexes for both Firefox and Chromium output, they are identical unless I miscounted I don't have internet explorer or chrome, I had a friend test pull up the link that has this code <?php $browsee = get_browser(null,true); $array = (array_slice($browsee,5,-24)); if(in_array("Firefox",$array)){ echo "your browser is firefox"; }else if(in_array("Chromium",$array)){ echo "your browser is chromium"; }else if(in_array("Chrome",$array)){ echo "your browser is chrome"; }else if(in_array("Explorer",$array)){ echo "your browser is Internet Explorer"; }else{ echo "your browser was not detected"; } echo '<br>'; echo '<br>'; print_r($browsee); ?> These are screenshots of the two browsers I use (I'm a Linux user) Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/ Share on other sites More sharing options...
Barand Posted January 12, 2015 Share Posted January 12, 2015 Can you show us the output from print_r($array) I'd do it myself but array_slice won't work on the images you so thoughtfully provided Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502728 Share on other sites More sharing options...
moose-en-a-gant Posted January 12, 2015 Author Share Posted January 12, 2015 what does this mean? I'd do it myself but array_slice won't work on the images you so thoughtfully provided Usually when I try to output that, I just get "boolean(true)" or 1 Here's the output your browser is chromiumArray ( [browser_name_regex] => §^mozilla/5\.0 \(.*linux x86_64.*\) applewebkit/.* \(khtml, like gecko\).*ubuntu.*chromium/39\..*chrome/.*safari/.*$§ [browser_name_pattern] => Mozilla/5.0 (*Linux x86_64*) AppleWebKit/* (KHTML, like Gecko)*Ubuntu*Chromium/39.*Chrome/*Safari/* [parent] => Chromium 39.0 [platform] => Ubuntu [comment] => Chromium 39.0 [browser] => Chromium [version] => 39.0 [majorver] => 39 [frames] => 1 [iframes] => 1 [tables] => 1 [cookies] => 1 [javascript] => 1 [javaapplets] => 1 [cssversion] => 3 [minorver] => 0 [platform_version] => unknown [alpha] => [beta] => [win16] => [win32] => [win64] => [backgroundsounds] => [vbscript] => [activexcontrols] => [ismobiledevice] => [istablet] => [issyndicationreader] => [crawler] => [aolversion] => 0 ) Array ( [browser] => Chromium ) from this code: <?php $browsee = get_browser(null,true); $array = (array_slice($browsee,5,-24)); if(in_array("Firefox",$array)){ echo "your browser is firefox"; }else if(in_array("Chromium",$array)){ echo "your browser is chromium"; }else if(in_array("Chrome",$array)){ echo "your browser is chrome"; }else if(in_array("Explorer",$array)){ echo "your browser is Internet Explorer"; }else{ echo "your browser was not detected"; } echo '<br>'; echo '<br>'; print_r($browsee); echo '<br>'; echo '<br>'; print_r($array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502730 Share on other sites More sharing options...
Barand Posted January 12, 2015 Share Posted January 12, 2015 (edited) What about the firefox array and the others that do not work? If you have a 30 element array then array_slice($array, 5, -24) leaves a single element $a = range(1,30); $array = array_slice($a, 5, -24, 1); echo '<pre>',print_r($array, true),'</pre>'; /* results ************** Array ( [5] => 6 ) ****************************/ So if that 1 element doesn't contain what you are looking for then it won't work. Why use array_slice() here anyway? Edited January 12, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502733 Share on other sites More sharing options...
moose-en-a-gant Posted January 13, 2015 Author Share Posted January 13, 2015 I'm specifically targetting the index [browser] What I'm really after is to use get_browser() to return the browser type and then knowing this I can do specific changes to a website I'm trying to accomplish it by string matching the browser names to the array output of get_browser() I have isolated the [browser] as to me this tells you the name of the browser so that is my I'm confused why the array_slice is not matching since it worked for one but not the others and the index count / position has not changed Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502736 Share on other sites More sharing options...
CroNiX Posted January 13, 2015 Share Posted January 13, 2015 (edited) Haven't tried, but according to the docs this should work: //returns array of browser attributes $browser = get_browser(null, true); //get the name of the browser from the $browser array, but force to lowercase for comparison $browser_name = strtolower($browser['browser']); if($browser_name == 'firefox'){ echo "your browser is firefox"; }else if($browser_name == "chromium"){ echo "your browser is chromium"; }else if($browser_name = "chrome")){ echo "your browser is chrome"; }else if($browser_name = "explorer"){ echo "your browser is Internet Explorer"; }else{ echo "your browser was not detected"; } I'm not sure why you're using array_slice for this... Edited January 13, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502737 Share on other sites More sharing options...
moose-en-a-gant Posted January 13, 2015 Author Share Posted January 13, 2015 Haven't tried, but according to the docs this should work: //returns array of browser attributes $browser = get_browser(null, true); //get the name of the browser from the $browser array, but force to lowercase for comparison $browser_name = strtolower($browser['browser']); if($browser_name == 'firefox'){ echo "your browser is firefox"; }else if($browser_name == "chromium"){ echo "your browser is chromium"; }else if($browser_name = "chrome")){ echo "your browser is chrome"; }else if($browser_name = "explorer"){ echo "your browser is Internet Explorer"; }else{ echo "your browser was not detected"; } I'm not sure why you're using array_slice for this... Ha wow that's great it works. I explained the array_slice above, it was to isolate the index that said the browser name from the two test browsers Chromium and Firefox in Linux, array index # 6 is the browser, I counted the brackets by hand haha, tap, tap, tap Anyway thank you for this, now I can fix my nightmare situation with my interface Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502738 Share on other sites More sharing options...
moose-en-a-gant Posted January 13, 2015 Author Share Posted January 13, 2015 Crap I do intend to mark CroNiX's answer as the solution but I don't know how it works Where is the comparison, I mean it is the == but is it the $browser['browser'] ? Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502739 Share on other sites More sharing options...
CroNiX Posted January 13, 2015 Share Posted January 13, 2015 Well I created a variable for that here and forced it to lower-case: $browser_name = strtolower($browser['browser']); so it's checking the $browser_name == lower-case browser names like 'firefox' instead of 'Firefox', which I think is would be more reliable. Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502740 Share on other sites More sharing options...
moose-en-a-gant Posted January 13, 2015 Author Share Posted January 13, 2015 Oh like probability The array or "string" to be checked comes from get_browser yeah this did cross my mind too but I had hoped the Capital first lettere rest lower case would have worked, since that is how they have the output shown At any rate, thank you Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502742 Share on other sites More sharing options...
moose-en-a-gant Posted January 13, 2015 Author Share Posted January 13, 2015 The internet catch doesn't seem to be triggering I have tried internet, net, explorer Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502745 Share on other sites More sharing options...
moose-en-a-gant Posted January 13, 2015 Author Share Posted January 13, 2015 (edited) Even better a photo! Yeah something seems off, my friend is testing internet explorer and this is the output your browser is chrome Array ( [browser_name_regex] => §^mozilla/4\.0 \(compatible; msie 8\.0.*; .*windows nt 6\.0.*wow64.*trident/4\.0.*$§ [browser_name_pattern] => Mozilla/4.0 (compatible; MSIE 8.0*; *Windows NT 6.0*WOW64*Trident/4.0* [parent] => IE 8.0 [platform] => WinVista [platform_version] => 6.0 [win32] => [win64] => 1 [comment] => IE 8.0 [browser] => IE [version] => 8.0 [majorver] => 8 [frames] => 1 [iframes] => 1 [tables] => 1 [cookies] => 1 [backgroundsounds] => 1 [javascript] => 1 [vbscript] => 1 [javaapplets] => 1 [activexcontrols] => 1 [cssversion] => 2 [minorver] => 0 [alpha] => [beta] => [win16] => [ismobiledevice] => [istablet] => [issyndicationreader] => [crawler] => [aolversion] => 0 ) ahhhh.... IE Edited January 13, 2015 by moose-en-a-gant Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502746 Share on other sites More sharing options...
moose-en-a-gant Posted January 13, 2015 Author Share Posted January 13, 2015 The internet explorer browser is still not getting detected properly I don't understand this $browser['browser'] I mean I think I do, it looks the same as $errors['username'] for example or $_SESSION['user'] It's just an array right? I tried to not check for non-caps using this line $browser_na = ($browser['browser']); I guess I'll look and see if I can find an internet explorer specific word that is lower case to use Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502749 Share on other sites More sharing options...
Psycho Posted January 13, 2015 Share Posted January 13, 2015 (edited) Cronix had a typo in the code he provided. Specifically there was this: }else if($browser_name = "chrome")){ echo "your browser is chrome"; That is trying to assign the value 'chrome' to the variable $browser_name (notice the single equal sign) instead of comparing them. Since the assignment always succeeds, the result is true and that condition is met. You could fix that with a double equal sign, but I think there is a better way to do the comparison that allows you to expand the list easily without writing more code. Just change the array at the top: //Create list of known browsers // -- Set key as the lowercase value found in the return value of get_browser // -- Set value to the name you would like to display $browserList = array( 'firefox'=>'Firefox', 'chromium'=>'Chromium', 'chrome'=>'Chrome', 'ie'=>'Internet Explorer' ); //returns array of browser attributes $browser = get_browser(null, true); //get the name of the browser from the $browser array, but force to lowercase for comparison $browser_name = strtolower($browser['browser']); //Check if returned browser name exists in the list of known browsers if(isset($browserList[$browser_name])) { $browserMsg = "Your browser is {$browserList[$browser_name]}"; } else { $browserMsg = "Your browser was not detected"; } echo $browserMsg; Edited January 13, 2015 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502775 Share on other sites More sharing options...
moose-en-a-gant Posted January 13, 2015 Author Share Posted January 13, 2015 (edited) Thank you for this, code blindness I suppose on my end Edited January 13, 2015 by moose-en-a-gant Quote Link to comment https://forums.phpfreaks.com/topic/293880-array_slice-not-working-properly/#findComment-1502792 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.