I can't seem to get my thoughts properly wrapped around this problem.

Scenario:

Searching an array of data for matches to keywords. Create a new array from the matches.

01. From 1 to 3 keywords possible. Match must have ALL keywords.

02. Case insensitive

03. $needle1, $needle2 and $needle3 represent the possible keywords

04. $old_haystack represents an element from the original array

05. $haystack represents lowercase version of $old_haystack

06. must be compatible with PHP versions 3, 4 and 5

I came up with this sledge-hammer approach, looping through all of elements of the original array and runing this function on each element.

However, I am sure there is a more elegant, refined and effective approach, but what is it?

<?PHP function validate_words($needle1, $needle2, $needle3, $old_haystack) { $haystack = strtolower($old_haystack); $new_element = ""; $kwl[0] = strlen(trim($needle1); $kwl[1] = strlen(trim($needle2); $kwl[2] = strlen(trim($needle3); $add_element = 0; if(($kwl[0]>0) AND ($kwl[1]>0) AND ($kwl[2]>0) AND (substr_count($haystack, $needle1)>0) AND (substr_count($haystack, $needle2)>0) AND (substr_count($haystack, $needle3)>0)) { // add element to new array return 1; } if(($kwl[0]>0) AND ($kwl[1]>0) AND ($kwl[2]<1) AND (substr_count($haystack, $needle1)>0) AND (substr_count($haystack, $needle2)>0)){ // add element to new array return 1; } if(($kwl[0]>0) AND ($kwl[1]<1) AND ($kwl[2]>0) AND (substr_count($haystack, $needle1)>0) AND (substr_count($haystack, $needle3)>0)) { // add element to new array return 1; } if(($kwl[0]>0) AND ($kwl[1]<1) AND ($kwl[2]<1) AND (substr_count($haystack, $needle1)>0)) { // add element to new array return 1; } if(($kwl[0]<1) AND ($kwl[1]>0) AND ($kwl[2]>0) AND (substr_count($haystack, $needle2)>0) AND (substr_count($haystack, $needle3)>0)) { // add element to new array return 1; } if(($kwl[0]><1) AND ($kwl[1]>0) AND ($kwl[2]<1) AND (substr_count($haystack, $needle2)>0)) { // add element to new array return 1; } if(($kwl[0]<1) AND ($kwl[1]<1) AND ($kwl[2]>0) AND (substr_count($haystack, $needle3)>0)) { // add element to new array return 1; } return 0; } ?>

Thanks,

Lite...