Skip to content

Instantly share code, notes, and snippets.

@ademalp
Last active December 3, 2022 08:06
Show Gist options
  • Save ademalp/4b825ff4d11f9fc2194f479a27fd80f2 to your computer and use it in GitHub Desktop.
Save ademalp/4b825ff4d11f9fc2194f479a27fd80f2 to your computer and use it in GitHub Desktop.
PHP str_contains_array
<?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;
}
<?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