note 113911 added to function.strpos
| From: | Lurvik | Date: | Mon, 16 Dec 2013 19:02:45 +0000 |
| Subject: | note 113911 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197953@lists.php.net to get a copy of this message | ||
Don't know if already posted this, but if I did this is an improvement.
This function will check if a string contains a needle. It _will_ work with arrays and
multidimensional arrays (I've tried with a > 16 dimensional array and had no problem).
<?php
function str_contains($haystack, $needles)
{
//If needles is an array
if(is_array($needles))
{
//go trough all the elements
foreach($needles as $needle)
{
//if the needle is also an array (ie needles is a multidimensional array)
if(is_array($needle))
{
//call this function again
if(str_contains($haystack, $needle))
{
//Will break out of loop and function.
return true;
}
return false;
}
//when the needle is NOT an array:
//Check if haystack contains the needle, will ignore case and check for whole words only
elseif(preg_match("/\b$needle\b/i", $haystack) !== 0)
{
return true;
}
}
}
//if $needles is not an array...
else
{
if(preg_match("/\b$needles\b/i", $haystack) !== 0)
{
return true;
}
}
return false;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/113911
Del: integrated -- https://master.php.net/note/delete/113911/integrated
Del: useless -- https://master.php.net/note/delete/113911/useless
Del: bad code -- https://master.php.net/note/delete/113911/bad+code
Del: spam -- https://master.php.net/note/delete/113911/spam
Del: non-english -- https://master.php.net/note/delete/113911/non-english
Del: in docs -- https://master.php.net/note/delete/113911/in+docs
Del: other reasons-- https://master.php.net/note/delete/113911
Reject -- https://master.php.net/note/reject/113911
Search -- https://master.php.net/manage/user-notes.php