ShadowIce Posted February 6, 2010 Share Posted February 6, 2010 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/ Share on other sites More sharing options...
premiso Posted February 6, 2010 Share Posted February 6, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1007999 Share on other sites More sharing options...
ShadowIce Posted February 6, 2010 Author Share Posted February 6, 2010 that didnt work. that returns <?php echo "browser: ".stristr($_SERVER['HTTP_USER_AGENT'], "Safari"); ?> browser:Safari/531.21.10 Not Safari how do i cut off the rest of the string? Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008041 Share on other sites More sharing options...
premiso Posted February 6, 2010 Share Posted February 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008050 Share on other sites More sharing options...
ShadowIce Posted February 6, 2010 Author Share Posted February 6, 2010 again. not what i meant i dont want to chop up the string im SEARCHING for. I just want to locate the ONE string "Safari" and chop off the rest of the string (the words after that string) Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008052 Share on other sites More sharing options...
premiso Posted February 6, 2010 Share Posted February 6, 2010 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008061 Share on other sites More sharing options...
ShadowIce Posted February 6, 2010 Author Share Posted February 6, 2010 now it returns: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/ i just want it to be the word Safari nothing before it, nothing after it. Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008063 Share on other sites More sharing options...
mattal999 Posted February 6, 2010 Share Posted February 6, 2010 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008064 Share on other sites More sharing options...
ShadowIce Posted February 6, 2010 Author Share Posted February 6, 2010 THANK you! Mattal999 has it! Thanks Mattal! Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008065 Share on other sites More sharing options...
Mchl Posted February 6, 2010 Share Posted February 6, 2010 LOL... just take premiso's first post, replace return true; with return 'Safari'; and you have same thing. Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008069 Share on other sites More sharing options...
premiso Posted February 6, 2010 Share Posted February 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008074 Share on other sites More sharing options...
ShadowIce Posted February 6, 2010 Author Share Posted February 6, 2010 thanks premiso thank u for trying ^^ Quote Link to comment https://forums.phpfreaks.com/topic/191166-please-help-detect-safari-browser-code-is-going-insane/#findComment-1008076 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.