1
$source = ['domain.com', 'subdomain.value.com'];
$str = ['value.com', 'blur.de', 'hey.as'];

How to check if any of $str is present in $source?

I've tried in_array or array_intersect but both seem to check for exact values, so none match.

In this example, it should match as 'value.com' is present in 'subdomain.value.com'.

1
  • you need something more complicated than in_array because there is a bit of extra matching work for the substrings. in_array and array_intersect do only exact matches. Importantly, to find the right solution-- should "subdomain.value" (or just "value", or just "in.va") match to "subdomain.value.com" ? Commented Apr 14, 2022 at 21:01

1 Answer 1

2
Answer recommended by PHP Collective

You could replace elements in $source with elements in $str and see how many replacements were made:

str_replace($str, '', $source, $count);
echo $count;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.