954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Search for a string in a variable---

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

cancer10
Posting Whiz in Training
234 posts since Dec 2004
Reputation Points: 58
Solved Threads: 1
 

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!

Stylish
Junior Poster
149 posts since May 2007
Reputation Points: 44
Solved Threads: 19
 

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
[PHP]
if (strstr('http://example.com/sports.ext', 'sports')) {
echo 'found';
} else {
echo 'not found';
}[/PHP]

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

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You