Forum Moderators: coopster

Message Too Old, No Replies

strstr - PHP detecting Gecko Revision?

Remove up to and including "rv:" and then remove from ") Gecko" on?

         

JAB Creations

8:40 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been wondering semi-aimlessly around php.net literally all day as I have a habit of figuring things out just before I post something but so far this has eluded me for the whole day.

I am pretty sure I want to use strstr to get...

1.8

out of...

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5

and I just want to associate whatever value (in this case "1.8") to the $gecko variable.

I'm not sure how to say I want to...
1.) Remove up to and including "rv:"
2.) Remove ") Gecko" and on till the end of the string.
3.) Reference the remaining string to the $gecko variable.

The one thing I do have declared is...

$ua = getenv("HTTP_USER_AGENT");

and that way I can understand this if we take $ua and I can reference the Gecko version from $gecko (meaning I'll understand it better on my own).

John

adb64

8:53 am on Jan 26, 2006 (gmt 0)

10+ Year Member



John,

Use strpos() to find it:


$gecko = "";
$rvpos = strpos($ua,'rv:');
if ($rvpos!== FALSE)
{
$rvend = strpos($ua,')',$rvpos);
if ($rvend!== FALSE)
{
$gecko = trim(substr($ua,$rvpos+3,$rvend-$rvpos-3));
}
}

Regards,
Arjan

JAB Creations

8:58 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Heya Arjan and thanks for the quick reply!

I tried this but I didn't get any output (or errors)? My UA is the default (double-checked before I replied).

<?php
$ua = getenv("HTTP_USER_AGENT");
$gecko = "";
$rvpos = strpos($ua,'rv:');
if ($rvpos!== FALSE)
{
$rvend = strpos($ua,')',$rvpos);
if ($rvend!== FALSE)
{
$gecko = trim(substr($ua,$rvpos+3,$rvend-$rvpos-3));
}
}
?>

adb64

9:12 am on Jan 26, 2006 (gmt 0)

10+ Year Member



John,

Well I didn't test it, just typed by heart.
Check the values of $rvpos and $rvend and check whether their value corresponds with the positions of 'rv:' and ')' in the string.

Arjan

adb64

9:23 am on Jan 26, 2006 (gmt 0)

10+ Year Member



I did a quick test and the code returned 1.8 in $gecko when I set $ua to "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5"
Does $ua has the correct value.

JAB Creations

9:25 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I have some sort of understanding...

I'm really not qualified but...

I think $rvpos might be the issue?

Do we want to throw this in to two phases perhaps? First take the variable and remove the beginning, output what we have, then do the same to the end?

I understand each part of the script in incremental logic at least. :)

John

adb64

9:28 am on Jan 26, 2006 (gmt 0)

10+ Year Member



I think your call getenv() is the problem. Use the global $_SERVER['HTTP_USER_AGENT'] instead.
You may also want to take a look at the get_browser() [nl2.php.net] function in PHP.

JAB Creations

9:35 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see this...
Mozilla [version] => 1.8

But I'm not sure how to call that specifically?

I also changed the env to the global and got the same non-result.

adb64

9:51 am on Jan 26, 2006 (gmt 0)

10+ Year Member



I don't understand why it won't work with you. What I did to test was set the string $ua fixed to "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5" instead of retrieving it from a function or variable.
When you print $ua does it contain the correct string? And when it is correct, what are the values of $rvpos and $rvend? That should give you a clue of where to look for the problem. With me it worked fine when $ua contains the correct string.

JAB Creations

10:14 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, I double tested this both locally and on my live site (hidden away) and nothing is working. Do you have it working locally? Maybe it's the PHP version or some kind of addon?

Here is what I'm testing just so we're on the same page. :)

<?php
$ua = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5";
echo $ua;
echo '<br />';
$gecko = "";
$rvpos = strpos($ua,'rv:');
if ($rvpos!== FALSE)
{
$rvend = strpos($ua,')',$rvpos);
if ($rvend!== FALSE)
{
$gecko = trim(substr($ua,$rvpos+3,$rvend-$rvpos-3));
}
}
?>

JAB Creations

10:18 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh wait, BIG DUUUUUUUUUUUUUUUUH!

We didn't echo/print $gecko. *smacks forhead*

<?php

$ua = $_SERVER['HTTP_USER_AGENT'];
echo $ua;
echo '<br />';
$gecko = "";
$rvpos = strpos($ua,'rv:');
if ($rvpos!== FALSE)
{
$rvend = strpos($ua,')',$rvpos);
if ($rvend!== FALSE)
{
$gecko = trim(substr($ua,$rvpos+3,$rvend-$rvpos-3));
}
}
echo $gecko;
?>

Kick ass! Thanks a ton Arjan! :)

John

adb64

10:45 am on Jan 26, 2006 (gmt 0)

10+ Year Member



I tested this locally with PHP 4.4.2. I copied your code and pasted it into a file locally:

<?php 
$ua = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5";
$gecko = "";
$rvpos = strpos($ua,'rv:');
if ($rvpos!== FALSE)
{
$rvend = strpos($ua,')',$rvpos);
if ($rvend!== FALSE)
{
$gecko = trim(substr($ua,$rvpos+3,$rvend-$rvpos-3));
}
}
echo "ua='$ua'\n";
echo "gecko='$gecko'\n";
echo "rvpos='$rvpos'\n";
echo "rvend='$rvend'\n";
?>

When running this I get the following output:

X-Powered-By: PHP/4.4.2
Content-type: text/html

ua='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5'
gecko='1.8'
rvpos='48'
rvend='54'

I have no idea what is wrong with your setup

[edit adb64]Oops, just saw your last post when I submitted mine. Great it works now :-)[/edit]