QUOTE(Transverse Styles @ Jun 6 2005, 06:18 PM)

CODE
<?php
$string_referrer = "altavista.";
$search_engines = array("altavista.", "google.", "msn.", "yahoo.");
if(in_array($string_referrer, $search_engines)) {
echo "Stuff that happens when the referrer is either Altavista, Google, MSN or Yahoo!";
}
?>
Use microtime to see if this approach is faster... although, it should be.
Hmm, for some reason, it doesn't work...
CODE
$arrSearchEngines=array("altavista.","google.","msn.","yahoo.");
if(in_array($strReferrer,$arrSearchEngines)) { ... }
Although the referrer contains "google.", the code inside the "if" statement is not executed.
QUOTE(Wombat @ Jun 6 2005, 06:35 PM)

As PHP treats non-zero as TRUE and 0 as FALSE I'd use substr_count instead of stristr. But performance-wise I doubt there'd be any difference.
As for code-wise, this is how I'd do it. It's a lot easier to read:
CODE
$isAltavista = substr_count($strReferrer,"altavista.");
$isGoogle = substr_count($strReferrer,"google.");
$isMSN = substr_count($strReferrer,"msn.");
$isYahoo = substr_count($strReferrer,"yahoo.");
if( $isAltavista || $isGoogle || $isMSN || $isYahoo ) {
// ... do stuff if is one of those engines
}
Thanks for this, but I'd rather use Transverse Styles' approach.

QUOTE(malikyte @ Jun 6 2005, 06:36 PM)

Rather than doing a case-insensitive search, why not just do a
strtolower() prior to calling either of the two methods?
(Doing that would also make Transverse Styles' approach work a bit better.)
As far as your actual question, I don't know what the best way of doing this would be as I'm not sure what form all of the variables used here are in, or what they're for... It's hard to look at a small piece of code and say whether or not it's "the best way" unless you can see the big picture.
-edit-
You'd also want to apply the stringtolower() on your $strReferrer variable for Wombat's example too.

In fact, I already do so:
CODE
$strReferrer=strtolower($_SERVER["HTTP_REFERER"]);
QUOTE(Transverse Styles @ Jun 6 2005, 06:40 PM)

We'd have to have more information to optimize his bit of code. After looking at this a bit more, I think that using an array is the best approach.
What I want to do is convert the JavaScript from ALA's
The Perfect 404 to PHP. This is what I have:
CODE
$strReferrer=strtolower($_SERVER["HTTP_REFERER"]);
if(strlen($strReferrer)==0) {
// User tried to access out-of-date site by using an outdated bookmark or mistyped URL - basically, no referrer was passed to the server
}
else {
if(!(stristr($strReferrer,"altavista.")===FALSE) || !(stristr($strReferrer,"google.")===FALSE) || !(stristr($strReferrer,"msn.")===FALSE) || !(stristr($strReferrer,"yahoo.")===FALSE)) {
$arrPath=explode("/",$strReferrer);
$arrParameters=explode("?",$strReferrer);
$arrSearchTerms=$arrParameters[1];
$arrParameters=explode("&",$arrSearchTerms);
$strSearchEngineAddress=$arrPath[2];
$arrQueryStrings[0]="q=";
$arrQueryStrings[1]="p=";
for($intCounter1=0;$intCounter1<sizeof($arrParameters);$intCounter1 ) {
for($intCounter2=0;$intCounter2<sizeof($arrQueryStrings);$intCounter2 ) {
$strQuerryString=$arrQueryStrings[$intCounter2];
if(!(stristr($arrParameters[$intCounter1],$strQuerryString)===FALSE)) {
$arrSearchTerms=$arrParameters[$intCounter1];
$arrSearchTerms=explode($strQuerryString,$arrSearchTerms);
$arrSearchTerms=$arrSearchTerms[1];
$arrSearchTerms=str_replace(" "," ",$arrSearchTerms);
}
}
}
// User came to an out-of-date site by using a search engine
}
}