Jump to content

array_slice not working properly


moose-en-a-gant

Recommended Posts

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)

 

 

post-174107-0-21801000-1421100406_thumb.png

 

post-174107-0-17409700-1421100419_thumb.png

Link to comment
Share on other sites

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 chromium

Array ( [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);
?>
Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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 by CroNiX
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Even better a photo!

 

post-174107-0-40890300-1421112415_thumb.jpg

 

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 by moose-en-a-gant
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.