Web Design for Beginners

Free top tips for web designers



Finding a word within a string

By James Middleton - Added 11th of January 2009

Now 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 & Links

Article written by James Middleton - www.webdesign-4-beginners.co.uk.

Information for Publishers

This article is copyrighted and you do not have consent to copy or redistribute it without written consent from the author.

Add your own comments

* Must complete
*
*
Please do not use HTML codes within this field

(Max chars: 1000)
Characters remaining:
*
Please type the above code into the box below
(case sensitive):

Please Note: All comments are reviewed before going live.



Web Design for Beginners is ©Copyright 2008 Turning Turnip Web Design - All rights reserved.

Warning: mysql_close(): no MySQL-Link resource supplied in /homepages/38/d234135144/htdocs/webdesign-4-beginners/inc/bot-half.php on line 19