开发者

Check if an item is in an array with a custom comparator in PHP

I have an array of root domains (eg google.com, somedomain.com, but not subdomain.domain.com) and i want to c开发者_开发技巧heck if an arbitrary domain has the same root domain. The arbitrary domain could be subdomain.domain.com and it would match with domain.com.

Previously I just filtered a list of input domains using array_udiff and a custom comparator function I wrote. I just switched to a method where each input domain has to be checked individually, I can't just check against a whole array.

Rather than do array_udiff with an array of size 1 and check to see if its size isn't 0 afterwards, or loop through the array and call the function manually, I was hoping that there was a function that already does this. PHP has an in_array function but it doesn't allow a custom comparator. Does such a function exist?


An alternative approach:

Store your root domains in a hash structure, that is an array with the domain names as keys. That way you get constant time lookups (e.g. via array_key_exists), which is significant, if you perform an operation over and over again.

As for the comparison, why not just strip the domain names to check of everything but the domain name?

  • How do you strip out the domain name from a URL in php?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜