note 113900 added to function.strpos
| From: | kiipa at live dot com | Date: | Sun, 15 Dec 2013 14:22:33 +0000 |
| Subject: | note 113900 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197937@lists.php.net to get a copy of this message | ||
So I needed a function to check if a string contains a string. But that also works with arrays. And
multidimensional arrays.
Well commented, because I'm sharing the source I use this in.
<?php
private static $found;
//Sees if a string contains another string, WORKS WITH 2D ARRAYS! Returns true or false.
public static function strcont($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
static::strcont($haystack, $needle);
}
//when the needle is NOT an array:
elseif(strpos($haystack, $needle) !== false)
{
//Set the bool to true (DO NOT CHANGE TO return true). IT WILL NOT WORK!
static::$found = true;
}
}
//Because we only set the bool if we found something we'll check if the bool is set.
if(isset(static::$found))
{
//Then return true.
return true;
}
return false;
}
//If the needles is not an array (this function can be used as strpos...)
else
{
if(strpos($haystack, $needles) !== false)
{
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/113900
Del: integrated -- https://master.php.net/note/delete/113900/integrated
Del: useless -- https://master.php.net/note/delete/113900/useless
Del: bad code -- https://master.php.net/note/delete/113900/bad+code
Del: spam -- https://master.php.net/note/delete/113900/spam
Del: non-english -- https://master.php.net/note/delete/113900/non-english
Del: in docs -- https://master.php.net/note/delete/113900/in+docs
Del: other reasons-- https://master.php.net/note/delete/113900
Reject -- https://master.php.net/note/reject/113900
Search -- https://master.php.net/manage/user-notes.php