Jump to content

PLEASE HELP! detect safari browser code is going INSANE! ><


Recommended Posts

Can someone please tell me how I can fix this so it detects ONLY the string "Safari" from within the user agent string?

 

<?php
function is_safari(){
if(strstr($_SERVER['HTTP_USER_AGENT'], "Safari")){
return true;
}else{
return false;
}
}
if(! is_safari()){
echo "<td>\r\n";
}else{
echo "hello!\r\n";
}
?>

You can try this:

 

function is_safari(){
    if(stristr($_SERVER['HTTP_USER_AGENT'], "Safari") !== false){
        return true;
    }

    return false;
}

 

Give that a try and see if it works for you. As just a more informative deal: http://www.useragentstring.com/pages/Safari/ may help you better determining the Safari (if it is needed).

stristr simply tells you the starting position of the word. If you want to return only part of the string you can use strpos for the S and i and then use substr to chop up the string.

 

But what would be the point of that, you already know the word...why chop up a string to get the same word? I think you are mis-informed on what the functions you are using do. I suggest reading the manuals on the ones I listed to see how they work and what they are suppose to do. Also you may also be leaving out exactly what you want the end result to me.

 

My assumption was you simply wanted to see if Safari was in the string if it was return true, which stristr accomplishes.

<?php

function is_safari($needle="Safari"){
$userAgent = $_SERVER['HTTP_USER_AGENT'];

    if (stristr($userAgent, $needle) !== false){
	// change the - (strlen($needle) + strlen($needle)) to just + strlen($needle) to include the needle in the output. 		
	return substr($userAgent, 0, strpos($userAgent, $needle) - strlen($needle) + strlen($needle));
    }

    return false;
}

echo is_safari();

?>

 

Perhaps that is what you are looking for (note the comment before the return statement for modification).

 

Edit: Added a minor fix.

There's no point in even doing all that. Just do this:

 

<?php

function is_safari($needle = "Safari") {
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    if(stristr($userAgent, $needle) !== false){
        return $needle;
    }
    return false;
}

echo is_safari();

?>

But what would be the point of that, you already know the word...why chop up a string to get the same word?

 

All I can say is...wow.

 

You did contradict yourself, however, which is why I posted the extensive function:

 

...string "Safari" and chop off the rest of the string (the words after that string)

 

"Chop off the rest" generally means the end of the string. Either way glad it was solved.

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.