Page 1 of 1

Searching for string within a string?

Posted: Sun Jan 27, 2008 2:02 pm
by PhpDog
What is an efficient way to find if $string1 contains the string "email or Email"?

Initially I was thing of:

if (substr_count($string1, "email") || substr_count($string1, "Email")
{
....
}

but is there a better way? (Just trying to add to my PHP education)

Re: Searching for string within a string?

Posted: Sun Jan 27, 2008 2:10 pm
by JAM

Re: Searching for string within a string?

Posted: Mon Jan 28, 2008 1:18 am
by jmut
while JAM's solution is tottaly valid, the "efficient way" would be using

Code: Select all

strpos()
In this regard, even when you replace a string it's good practice (faster) to first check if the string to replace exists at all - using strpos.

Re: Searching for string within a string?

Posted: Mon Jan 28, 2008 3:26 am
by PhpDog
jmut wrote:while JAM's solution is tottaly valid, the "efficient way" would be using

Code: Select all

strpos()
In this regard, even when you replace a string it's good practice (faster) to first check if the string to replace exists at all - using strpos.
Thanks for that.

Re: Searching for string within a string?

Posted: Mon Jan 28, 2008 5:10 am
by JAM
jmut wrote:while JAM's solution is tottaly valid, the "efficient way" would be using

Code: Select all

strpos()
In this regard, even when you replace a string it's good practice (faster) to first check if the string to replace exists at all - using strpos.
Of course are you right! It was me that posted the incorrect address for some odd reason. :oops: