Re: Searching in a string

From: Date: Sun, 10 Sep 2000 06:45:38 +0000
Subject: Re: Searching in a string
Groups: php.general 
Request: Send a blank email to php-general+get-16038@lists.php.net to get a copy of this message
Try the strstr() function. It takes two arguments, the string you're looking in, and the string you're looking for. If the second argument can be found in the first argument, it returns the portion of the first argument that starts with the second argument. If the string can't be found, the function returns false. For example, if your variables are $haystack = 'hello,there,world'; $needle_1 = 'world'; $needle_2 = 'there'; $needle_3 = 'cruel'; then the result of strstr($haystack, $needle_1) will be 'world' strstr($haystack, $needle_2) will be 'there,world' and the result of strstr($haystack, $needle_3) will be a false value You can ignore the actual contents of the string that's returned when this succeeds, and use it as a true/false test in an if statement. (You could also use strpos(), which returns the position where the string is found, but that will return 0 when the "needle" is at the beginning of the "haystack", which is tricky to distinguish from failure.) I assumed that the commas in 'hello,there,world' had no special meaning, and that you might as well be searching for "re,wo" as "world". If you mean the commas as delimiters, you might want the explode() function. --tim On Sun, 10 Sep 2000 01:20:49 -0500, Jeff Lacy wrote: > Hello. I have a string, let's say $string and that has several things in > it. So, $string=hello,there,world;. I would like to know if I can do a > simple if statement to see if world is in $string. world might not always > be in $string, so I need to make sure. How can I do this? Thank you in > advance. > > PLEASE EMAIL ME A REPLY. MS Outlook Express will DIE after I visit the > php.general news group. It will just freeze. I can look at the posts, but > I can't look at anything else without rebooting. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > _______________________________________________________ Say Bye to Slow Internet! http://www.home.com/xinbox/signup.html

« previous php.general (#16038) next »