Search for a string in a variable---

Is there a way to find a particular string in php.

For example:

I want php to echo "Found" if the word "sports" in found in this url http://yahoo.com/sports/soccer/index.html

if not then it should echo "bad Luck"


Is there any pre-defined function for this in php?


Thanx

Recommended Answers

All 2 Replies

preg_match sounds like what you want.

More info can be found here:
http://us2.php.net/manual/en/function.preg-match.php

Good luck!

You can use preg_match if you're going to search for a pattern in a string.

However, if you're going to look for just a string, then you should use a function such as strstr() or stristr()

eg: case sensitive

if (strstr('http://example.com/sports.ext', 'sports')) {
   echo 'found';
} else {
   echo 'not found';
}

preg_match() is much slower than strstr() and also uses more resources as it matches a pattern.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.