String contains

Searches for a string in an other string - returns true or false.
Build with simple functions.

Snippet information

Author:
Jonas John

License:
Public Domain

Language:
PHP

Created:
03/28/2006

Updated:
03/28/2006

Tags:
, ,


function contains($str, $content, $ignorecase=true){
    if ($ignorecase){
        $str = strtolower($str);
        $content = strtolower($content);
    }  
    return strpos($content,$str) ? true : false;
}


Found a bug? Or do you have a better solution for this?
Feel free to leave a message:

Add a comment


Leave a comment

rethab January 17, 2010 at 17:27
I just fixed it by modifying like this:
function str_contains($haystack, $needle, $ignorecase = true){
if ($ignorecase){
$haystack = strtolower($haystack);
$needle = strtolower($needle);
}
return strpos($haystack,$needle)!==false;
}
Piet November 15, 2008 at 22:05
iet November 15, 2008 at 21:53
Here is what the function should look like:

function contains($str, $content, $ignorecase=true)
{
if ($ignorecase)
{
$str = strtolower($str);
$content = strtolower($content);
}
return (strpos($content,$str) !== false) ? true : false;
}

That is because strpos() always use the === or !== operator to test for equality - as explained here:
http://www.php-mysql-tutorial.com/qna/detail/php-find-substring.php
Piet November 15, 2008 at 21:53
Here is what the function should look like:

function contains($str, $content, $ignorecase=true)
{
if ($ignorecase)
{
$str = strtolower($str);
$content = strtolower($content);
}
return (strpos($content,$str) !== false) ? true : false;
}
mizar September 24, 2008 at 10:41
Please rewrite your script, because it clearly does not work if the string contained starts from index 0. In that case the return will be false.
Reid June 05, 2008 at 01:02
The code my previous comment referenced won't work if there are more than one instance of the string.

This works for me:
return substr_count($haystack, $needle) >= 1 ? true : false;
Reid June 04, 2008 at 23:34
I don't think this function will work if the needle starts in the first character of the haystack.

http://us.php.net/manual/en/function.strpos.php#63539
muxil April 23, 2008 at 17:15
php type system sux ... personally, i can't trust the validation. better use this:

function str_contains($str, $content, $ignorecase=true){
$retval = false;

if ($ignorecase)
{
$str = strtolower($str);
$content = strtolower($content);
}

// php type system sucks so we may need a "special check"...
$_strpos = strpos($str, $content);
if ( $_strpos === 0 || $_strpos > 0 ) $retval = true;

return $retval;
}
muxil April 23, 2008 at 17:15
php type system sux ... personally, i can't trust the validation. better use this:

function str_contains($str, $content, $ignorecase=true){
$retval = false;

if ($ignorecase)
{
$str = strtolower($str);
$content = strtolower($content);
}

// php type system sucks so we may need a "special check"...
$_strpos = strpos($str, $content);
if ( $_strpos === 0 || $_strpos > 0 ) $retval = true;

return $retval;
}
muxil April 23, 2008 at 17:13
obrigado
lixum February 27, 2008 at 15:55
php type system sux ... personally, i can't trust the validation. better use this:

function str_contains($str, $content, $ignorecase=true){
$retval = false;

if ($ignorecase)
{
$str = strtolower($str);
$content = strtolower($content);
}

// php type system sucks so we may need a "special check"...
$_strpos = strpos($str, $content);
if ( $_strpos === 0 || $_strpos > 0 ) $retval = true;

return $retval;
}