Search for a string in a variable---

Reply

Join Date: Dec 2004
Posts: 234
Reputation: cancer10 is an unknown quantity at this point 
Solved Threads: 0
cancer10's Avatar
cancer10 cancer10 is offline Offline
Posting Whiz in Training

Search for a string in a variable---

 
0
  #1
May 9th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 146
Reputation: Stylish is an unknown quantity at this point 
Solved Threads: 14
Stylish's Avatar
Stylish Stylish is offline Offline
Junior Poster

Re: Search for a string in a variable---

 
0
  #2
May 9th, 2007
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!
I NEED AN ADULT!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Search for a string in a variable---

 
0
  #3
May 11th, 2007
Originally Posted by Stylish View Post
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.
Last edited by digital-ether; May 11th, 2007 at 8:09 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC