note 113900 deleted from function.strpos by cmb
| From: | cmb@php.net | Date: | Sun, 02 Jun 2019 08:03:56 +0000 |
| Subject: | note 113900 deleted from function.strpos by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211169@lists.php.net to get a copy of this message | ||
Note Submitter: kiipa at live dot com
----
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;
}
}
?>