String contains
Searches for a string in an other string - returns true or false.
Build with simple functions.
Author:
Jonas John
License:
Public Domain
Language:
PHP
Created:
03/28/2006
Updated:
03/28/2006
Tags:
string functions, find, search
function contains($str, $content, $ignorecase=true){ if ($ignorecase){ $str = strtolower($str); $content = strtolower($content); } return strpos($content,$str) ? true : false; }
if (contains("hello", "hello world")){ print "Yes"; }
Feel free to leave a message:
Add a comment
function str_contains($haystack, $needle, $ignorecase = true){
if ($ignorecase){
$haystack = strtolower($haystack);
$needle = strtolower($needle);
}
return strpos($haystack,$needle)!==false;
}
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
function contains($str, $content, $ignorecase=true)
{
if ($ignorecase)
{
$str = strtolower($str);
$content = strtolower($content);
}
return (strpos($content,$str) !== false) ? true : false;
}
This works for me:
return substr_count($haystack, $needle) >= 1 ? true : false;
http://us.php.net/manual/en/function.strpos.php#63539
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;
}
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;
}
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;
}