Last active
December 3, 2022 08:06
-
-
Save ademalp/4b825ff4d11f9fc2194f479a27fd80f2 to your computer and use it in GitHub Desktop.
PHP str_contains_array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function str_contains_array($haystacks, $needles) | |
{ | |
if (is_array($haystacks) && is_array($needles)) { | |
foreach ($haystacks as $haystack) { | |
foreach ($needles as $needle) { | |
if (mb_strpos($haystack, $needle) !== false) { | |
return true; | |
} | |
} | |
} | |
} | |
if (is_array($haystacks) && !is_array($needles)) { | |
foreach ($haystacks as $haystack) { | |
if (mb_strpos($haystack, $needles) !== false) { | |
return true; | |
} | |
} | |
} | |
if (!is_array($haystacks) && is_array($needles)) { | |
foreach ($needles as $needle) { | |
if (mb_strpos($haystacks, $needle) !== false) { | |
return true; | |
} | |
} | |
} | |
if (!is_array($haystacks) && !is_array($needles)) { | |
if (mb_strpos($haystacks, $needles) !== false) { | |
return true; | |
} | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function str_contains_array_alternative($haystacks, $needles) | |
{ | |
if (!is_array($haystacks)) { | |
$haystacks = [$haystacks]; | |
} | |
if (!is_array($needles)) { | |
$needles = [$needles]; | |
} | |
foreach ($haystacks as $haystack) { | |
foreach ($needles as $needle) { | |
if (mb_strpos($haystack, $needle) !== false) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment