PHP check string exist in string

PHP check if string contains specific words

The correct way to write the statement to check if string contains specific words is use strpos function.
Strpos is used to find the occurrence of one string inside other.

$str = 'She is going to public library';
//check if 'public' exist
if (strpos($str,'public') !== false)
	echo 'String public exist!';

Note that the use of !== false is deliberate; strpos returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn’t found. Since 0 is a valid offset and 0 is “falsey”, we can’t use simpler constructs like !strpos($str,’public’).

By Keenlio, November 17, 2014

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × = twenty five

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>