In_array "LIKE" function?

Hi,

I have a “blacklist” of all disallowed websites. The blacklist is stored in a CONF (“configuration”) array and each website is separated by a line break ("
"). To see if a website is in the blacklist I run this code:


$blacklisted = explode("\
", $bwclass->conf['website_blacklist']);
if (in_array($mywebsitevar, $blacklisted))
{
    echo 'You have been blacklisted.';
}

Here’s the thing. If a user enters in “http://mysite.com” as their website and “mysite.com” is in the blacklist, it won’t match. Why? Because the URL in the blacklist isn’t containing the http://. So, my question is, is there an array function that you can run to see if a string is contained WITHIN each element of an array? It’s like in_array, except just not an exact match. Is there anyway you know how to do this without doing a foreach() and ereg() on each element in an array?

Thanks for the ideas in advanced.

Not that I know of. But don’t be afraid to write your own. Predefined functions are great, but they don’t cover everything. Just do like you said and write a foreach loop. Though, I’ll recommend forgetting ereg and using preg because it’s more standard and much faster.

Be better to strip and add http:// during the foreach

This should work…


<?php
function similar_in_array( $sNeedle , $aHaystack )
{
    
    foreach ($aHaystack as $sKey)
    {
        if( stripos( strtolower($sKey) , strtolower($sNeedle) ) !== false )
        {
            return true;
        }
    }
    
    return false;
}
?>

Custom function’s and/or classes are the basis of any successful application, don’t be afraid of writing them, the’re great fun!:smiley:

Thanks, that works! :smiley: I knew I’d probably have to end up writing a custom function. I’m never opposed to that… I was just wondering if there was another function out there.

Case solved! :slight_smile: Thanks again.

stripos is case insensitive. No need to do strtolower.