Introduction

Finding a string within a string is very easy in PHP. You just have to think of a clear logic to do it. You can use PHP’s built-in strpos() function which can be used to find the occurrence of one string inside another.

Example

$a = 'How are you?';

if (strpos($a, 'are') !== false) 
{
    // Match found
    echo 'true';
}

Important Note

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($a, 'are').

Leave a Reply

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

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=""> <s> <strike> <strong>