Page 2 of 2

Posted: Sat May 20, 2006 3:30 pm
by timvw
aerodromoi wrote: Then why am I using css hacks and a javascript workaround for a (w3c compliant) website?
The answer is to be found in the $browser array :)
I don't know why you produce hacks, since the real solution would be to fix the browser...

Posted: Sat May 20, 2006 3:33 pm
by timvw
JAB Creations wrote:It's being able to detect whether the part of the useragent exists (from the array) compared to the string (the useragent).
http://www.php.net/substr
http://www.php.net/preg_match

Posted: Sat May 20, 2006 4:18 pm
by aerodromoi
JAB Creations wrote:Gah...

I'm not trying to detect various browsers or useragents. I'm just using that method because I know how to use it and test it properly.

It's being able to detect whether the part of the useragent exists (from the array) compared to the string (the useragent).

Honestly, I titled this thread comparing a string to a variable, not browser detection. I truly appreciate the help but if we can't stay focused on the topic at hand I see no point to be had.

If someone has a different method other then browser matching fine, I just want to learn how to compare an array to variable! :roll:
Focusing on the topic also means knowing what to achieve. :evil:

Comparing an array to a variable... An array is a series of data elements - but you never mentioned whether you want to match numbers, strings, boolean data or other arrays (going x-dimensional). So what method are you talking about? in_array was mentioned by timvw. Although it does not get us any further in this example, it is a perfectly valid answer to your question.

Since you're already talking about other functionalities - what are you trying to achieve? In this example it is obviously not sufficient to know whether string_a == string_b. Actually, we're looking for specific substrings, thus we are comparing two strings. So where's the focus now....

Here's one approach

Code: Select all

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$browser   = array('MSIE','Opera','Firefox');
$i         = 0;
$found     = false;

while (!$found && $i<count($browser)) {
  $found = ereg($browser[$i], $useragent);
  $i++;
}

if ($found){ echo "found";}
else{ echo "not found";}
?>


aerodromoi

Posted: Sat May 20, 2006 4:35 pm
by JAB Creations
That works great, thank you.

The problem on this forum is extremism. Either people go WAY off topic or people are too specific. I did not know by programmer's speak that my question had been answered. The reason why was that the script did not work, so my question (as in non-programmer's normal speak) was not answered. Like I said before extremism...people here are great for trying to help, but need to read posts from the perspective of the person asking the question. I clearly stated what I wanted to do from the start.

Anyway now that I have a working example the links that you posted DO serve a purpose. Otherwise I'd be just as lost as before.

I don't suppose there is a simpler way of comparing the variable and array mostly with just operators though? Don't kill yourselves if there isn't.

Again thanks...

Posted: Sat May 20, 2006 4:44 pm
by timvw
JAB Creations wrote:I don't suppose there is a simpler way of comparing the variable and array mostly with just operators though? Don't kill yourselves if there isn't.
Assuming you have an array with patterns that you want to match... You could generate the regular expression as following:

(untested)

Code: Select all

$subject = 'Microsoft Internet Explorer/4.0b1 (Windows 95)';
$patterns = array('MSIE', 'Opera', 'Firefox');
$regular_expression_pattern = implode('|', $patterns);
if (preg_match('#(' . $regular_expression_pattern . ')#i', $subject, $matches)) {
  echo 'I found ' . $matches[1] . ' in the subject: ' . $subject;
} else {
  echo 'I did not find a match in the subject: ' . $subject;
}

(Imho the real problem is that we don't have a crystal ball and can't (always) guess what someone's problem is... And i'm sure we all have been in that situation where it was hard to explain what the problem really was... )

Posted: Sat May 20, 2006 4:46 pm
by nielsene
Please correct me if I'm also missing the point:

You want to check if any of a set (array) of target substrings are in a given string.

There's no primitive/singular operator that will give you that. aerodromoi's approach works; you might also be able to come up with a fewer lines of code/more elegant solution in some people's eyes by playing with array_filter or array_map.

Posted: Sat May 20, 2006 5:29 pm
by JAB Creations
My array contains various values (browser names). If those values match the user agent I want to echo 200, otherwise I'd echo 403.

These are partial matches.

= assigns a variable
== partial matches
=== absolute match

I was using == originally when I posted. I'm only looking to make a positive match between part of the useragent in this case, not the whole useragent (the partial match).

I'm not naturally gifted at programming so I don't speak the tongue. You'll have to deal with my plain English skills instead, sorry! :lol:

Posted: Sat May 20, 2006 8:58 pm
by yum-jelly
Just use string functions....

Code: Select all

<?

$ok = false;

$ua = strtolower ( $_SERVER['HTTP_USER_AGENT'] ); // php 5 you can just use stripos for the if() in the for() loop, strtolower would not be used

$allowed = array ( 'opera', 'msie', 'firefox' );

for ( $i = 0; $i < sizeof ( $allowed ); $i++ )
{
	if ( strpos ( $ua, $allowed[$i] ) !== false )
	{
		$ok = true;
		break;
	}
}

if ( $ok )
{
	echo '200<br />';
	echo $ua;
}
else
{
	header ( 'HTTP/1.0 403' );
	echo '403<br />';
	echo $ua;
	exit (); 
}
?>

yj!

Posted: Sat May 20, 2006 10:01 pm
by nielsene
JAB Creations wrote: These are partial matches.

= assigns a variable
== partial matches
=== absolute match

I was using == originally when I posted. I'm only looking to make a positive match between part of the useragent in this case, not
OK, here is where it looks like you are a little confused.

Yes "=" assigns a variable.

"==" tests for "equality", not a partial match. Equality in most programming arenas mean that the two items have the same "value". If you are comparing strings, '==' is already doing an exact match.

"===" tests for "equivalence". This is a stricter definition of equality that tests if both things are the same type, have the same value, and (often) refers to the same location in memory (in the case of objects). In PHP 0==False=="", but none of them are "===" as they are different types.

you can do a partial match in strings using
if (FALSE!==strpos($wholeString,$searchString))
strpos returns the location where the searchString starts in the whole string. If the search string is the beginning of the whole string it will return '0', if its not found, it will return false (Here is where its important that FALSE!==0)