Web Design for BeginnersFree top tips for web designers |
Finding a word within a stringBy James Middleton - Added 11th of January 2009Now that we know how to hold words as variables or 'strings', let's see if we can use an 'if' statement to find a certain word within a given string: /// The following uses a 'Needle in a haystack' principle. $word = "PHP"; $sentence = "PHP is such an elegant language."; if(strstr($sentence ,$word)) { echo "Found a PHP!"; } The native PHP strstr function looks for the '$word' value in the '$sentence' string. The Above example is case sensitive. If you wanted to perform this search as a case insensitive, substitute 'strstr' with 'stristr'. Credits & LinksArticle written by James Middleton - www.webdesign-4-beginners.co.uk.Information for PublishersThis article is copyrighted and you do not have consent to copy or redistribute it without written consent from the author.Add your own comments |
|
Web Design for Beginners is ©Copyright 2008 Turning Turnip Web Design - All rights reserved.
|