I am working on a quiz program. I need to be able to search for sting ignoring case. The answer on the system may be "Shielded Cable" but I would like "shielded cable", "Shielded CABLE" and even "ShieldeD cable" to be acceptable also.

I currently use:

$q_an[$i]=array_search($answer[$i],$q_answer_decoded);

where $answer[$i] is the answer that the user typed in

$q_answer_decoded is an array of the possible correct answers

$q_an[$i] becomes the position of the answer within the array (or null if the answer is not correct)

but this will only work if both are identical in both case and content.

Any ideas?

Thank you.

Brian

    If you do a strtolower() on both the array-values and the search string, then compare, it should produce the desired result.

    <?PHP
    ## You'll need to strtolower the search-string as well ##
    $q_an[$i]=array_search(strtolower($answer[$i]),$q_answer_decoded);
    ?>
    

    knutm

      Found this on the [man]in_array[/man] page:

      function is_in_array($str, $array) {
       return preg_grep('/^' . preg_quote($str, '/') . '$/i', $array);
      }
      

      array_keys() the result of that and you get what your case-insensitive array_search() would have returned.

        Thank you for these comments but they don't really solve my problem.

        My array of answers has items that should be capitalized and I want to retain the capitalization for future printout and display. The correct answers are in an array because my program allows multiple correct answers to a single question. By using the search_array function I find if the user's answer is one of the acceptable answers and the result is that answer's key within the array. If the result is null I know the answer is incorrect.

        Users, however, often ignore capitalization or will enter in all capitals. I am hoping to find a procedure to allow that comparison without altering the capitalization used in the correct answer array.

        In my perfect wish list it would also ignore differences in spacing. e.g. "oxygen free copper" and "Oxygen free Copper" and "OXYGEN FREE COPPER" and "Oxygen &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Free&nbsp;&nbsp;&nbsp;copper" would all be acceptable if the correct answer on file is "Oxygen Free Copper"

        I may be asking for too much but this seems like a situation that would come up fairly often. Has anyone else run into this issue.

        Brian

          Preserve the original answer array and make a copy of it that you will remove all capital letters (strtolower)...

            I tried using $q_an[$i]=array_search(strtolower($answer[$i]),strtolower($q_answer_decoded));

            It doesn't work. Is there a way to use strtolower to affect an entire array?

            Brian

              You have to make a loop...

              Something like :

              foreach($arrMyArray as $strKey => $strValue) {
                   $arrMyArray[$strKey] = strtolower($strValue);
              }
              

              Only works in $arrMyArray has one dimension...

                Originally posted by birwin
                Thank you for these comments but they don't really solve my problem.

                My array of answers has items that should be capitalized and I want to retain the capitalization for future printout and display.

                This will happen with the preg_grep solution given above; I mentioned array_keys() because you had said that that's what you were after, but array_values() will get the - er - array values (retaining the capitalisation they had in the original array). I explicitly had the task of retaining the capitalisation in the original array in mind, which is one reason I discarded my first thought of using strtolower().

                  Write a Reply...