p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   PHP How-To (http://p2p.wrox.com/forumdisplay.php?f=98)
-   -   Search string (http://p2p.wrox.com/showthread.php?t=29942)

Mantis May 31st, 2005 03:01 PM

Search string
 
Something quite easy (if you know how to.. which officialy I don't!!)

I'm looking at isolating the browser name from the HTTP_USER_AGENT ..
Anyone know how to do this?

I'm looking for something like:
$Sname=$_SERVER['HTTP_USER_AGENT'];
if (stringsearch($Sname)="FireFox") {firefox=1;}
if (stringsearch($Sname)="Netscape") {Netscape=1;}
if (stringsearch($Sname)="MSIE") {MS=1;}

Thanks

anshul June 1st, 2005 04:31 AM

You need use strpos()
See a similar complete example http://www.mediasworks.com/scripts/

Mantis June 1st, 2005 04:41 AM

thats looking good - thks :)

Mantis June 1st, 2005 05:21 AM

A quicky .. I think there's a bug in PHP using strpos() - if anyone wants to check it and post it to Mr PHP

if your string="BlackBerry3270/5.3.1" and you do:
if(strpos(string, "BlackBerry") == true) { echo "Blackberry web browser detected"; }
That doesn't work.. However if you do:
if(strpos(string, "lackBerry") == true) { echo "Blackberry web browser detected"; }
That work fine.. so officialy strpos() isn't checking the 1st caracter...

anshul June 2nd, 2005 01:27 AM

True.

echo strpos($string, "BlackBerry"); // displays '0'
echo strpos($string, "BBlackBerry"); // displays 'nothing', NULL?

I tried to comapre it with 'NULL', still, it not yields!!

so,
Quote:

quote:if(gettype(strpos($string, "BlackBerry"))=="integer")
    echo "Blackberry web browser detected";

is fine as strpos() returns string position in integer, and NULL? when it not finds..

Alternatively, you may use preg_match() or preg_match_all() functions for absolute-n-powerful matching; these are PHP functions supporting PCRE. It'll take you a while to learn regex ( if you don't know ), but it's a worthy stuff, to invest time.


Mantis June 2nd, 2005 03:30 AM

I just changed $String=" ".$_SERVER['HTTP_USER_AGENT'];
I'm not for complication ;)


All times are GMT -4. The time now is 10:58 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.