Help - Search - Members - Calendar
Full Version: Check if string A contains string B using PHP
Invision Power Services > Community Forums > Community Web Design and Coding
Sebastian Mares
Hi folks!

What would I have to code to check if string A contains string B in PHP? Currently, I have the following:

CODE
if(!(stristr($strReferrer,"altavista.")===FALSE) || !(stristr($strReferrer,"google.")===FALSE) || !(stristr($strReferrer,"msn.")===FALSE) || !(stristr($strReferrer,"yahoo.")===FALSE)) {
  Stuff that happens when the referrer is either Altavista, Google, MSN or Yahoo!
}

...

if(!(stristr($arrParameters[$intCounter1],$strQuerryString)===FALSE)) { ... }


This works, but I am not sure if that is the correct approach (especially the "! ... === FALSE").

Regards,
Sebastian
princetontiger
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.
Wombat
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
}
Brendon Koz
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. original.gif
Wombat
QUOTE(malikyte @ Jun 6 2005, 05:36 PM) *
-edit-
You'd also want to apply the stringtolower() on your $strReferrer variable for Wombat's example too. original.gif


Thanks! Didn't notice that! thumbsup.gif
princetontiger
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.
Sebastian Mares
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. original.gif

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. original.gif


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
}
}
Wombat
QUOTE(Transverse Styles @ Jun 6 2005, 05: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.


But, $strReferrer which I assume could be the HTTP_REFERRER (i.e. the referring page) which could be:

http://www.altavista.com/web/results?itag=...est&kgs=1&kls=0
http://www.google.com/search?client=safari...=UTF-8&oe=UTF-8
http://www.google.co.uk/search?hl=en&q=tes...le Search&meta=://http://www.google.co.uk/search?hl=e...earch&meta=
http://search.yahoo.com/search?p=test&...-t&toggle=1
http://uk.search.yahoo.com/search?fr=fp-pu...t&ei=ISO-8859-1
etc.
etc.

in_array only matches if a given key is an entry of the array.

$array("key", "rubbish");

in_array("key", $array); // Returns true
in_array("keys", $array); // Returns false
Sebastian Mares
QUOTE(Wombat @ Jun 6 2005, 06:51 PM) *
in_array only matches if a given key is an entry of the array.

$array("key", "rubbish");

in_array("key", $array); // Returns true
in_array("keys", $array); // Returns false


That explains why it doesn't work then. original.gif
_
CODE
$arr = array('google.', 'msn.', 'altavista.');
for($a = 0; $a < count($arr); $a)
{
if(strpos($referer, $arr[$a]))
{
echo "Matched as a search engine.";
}
else
{
echo "Not a search engine.";
}
}


That should do, not sure if it's the fastest though.
Also, '$anything !== false' should be the same as '!($anything)===false'.
Sebastian Mares
Thanks for the help. I simply changed the !(stristr...===FALSE) part to (strpos!==FALSE).
princetontiger
So you found a solution?
Sebastian Mares
Well, it always worked, but I was wondering how to make it faster. I don't think that using a loop will result in any improvements, so that's why I simply used !== rather than !expression===FALSE.
princetontiger
You could optimize it a bit more... maybe use a case insensitive preg match with an array?
Wombat
QUOTE(Transverse Styles @ Jun 6 2005, 08:33 PM) *
You could optimize it a bit more... maybe use a case insensitive preg match with an array?


You're not going to get any tangible optimization out of such a trivial piece of code.
Sebastian Mares
QUOTE(Wombat @ Jun 6 2005, 11:18 PM) *
You're not going to get any tangible optimization out of such a trivial piece of code.


laughing.gif

Really, the only thing I wanted to know is if my code above can be written in a better was since the NOT FALSE sounded a bit odd.
Brendon Koz
...plus, if he already does a strtolower(), there's no reason to do a case-insensitive match at all anyway. That was the point of it (my suggestion) for speed/efficiency, to not have to later on.
princetontiger
I say go for the array!
Rikki
QUOTE(Transverse Styles @ Jun 7 2005, 01:35 AM) *
I say go for the array!


Did you not understand, it won't work tongue.gif

in_array checks for array values, not whether a string forms part of an array value.
princetontiger
Oh, I see why. I missed a few posts!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.