Hello. Quick question. I have several thousand emails pulled in from several email boxes throuph php imap and I need to search each subject with an array of search terms which returns a bitmask, one bit for each index of the array. 1 = found, 0 = not found.

So, if I have the phrase "my car is red" and I pass in the array "car,blue,red,green,sky,my,house,is" I will get a bitmask back of "10100101".

my solution:

public function stringSearchReturnBitMask(array $arrSearch, $strString) {
    $strReturn = "";
    foreach($arrSearch as $strSearch) {
        $strReturn .= stripos($strString, $strSearch) !== false?"1":"0";
    }

    return $strReturn;
}

Is there a more efficient way of doing this? I don't like the idea of a loop nested in a loop but I'm not aware of a preg_match solution that will do it for me and less taxing. I was thinking of imploding the array with "|" into a preg_match type function but how would I have it return a bitmask and would it actually be more efficient?

Member Avatar for diafol

I think a regex in preg_match would be best option. preg_* are slow, but quicker than nested loops I think. BUT getting bitmask output - hmm - have to think about that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.