Searching for string within a string?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PhpDog
Forum Commoner
Posts: 58
Joined: Mon Jan 14, 2008 10:23 am

Searching for string within a string?

Post 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)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Searching for string within a string?

Post by JAM »

jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Searching for string within a string?

Post 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.
PhpDog
Forum Commoner
Posts: 58
Joined: Mon Jan 14, 2008 10:23 am

Re: Searching for string within a string?

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Searching for string within a string?

Post 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:
Post Reply