Tuesday, November 6, 2007

How to check if a text exists in a string

How to check if a substring exists in a string in PHP



Having a string:

$mystring = 'teststringforme';

I want to know if, for example, 'for' exists in $mystring. Using PHP function strpos, it's very easy.

if( strpos( $mystring, 'for' ) !== false )
{
echo 'string exists';
}
else
{
echo 'string does not exist';
}


Find if a text exists in a string using PHP

1 comment:

Anonymous said...

Awesome