Search Results

'); } $flower_dname = []; while (fscanf($handle, '%s', $cur_dir_name)) { // flower_dirs.txt is treated as trusted application data. $flower_dname[] = $cur_dir_name; } fclose($handle); // Read the keywords for each species and store them. $keyword_list = []; foreach ($flower_dname as $cur_dir_name) { $keyword_file = $cur_dir_name . '/keywords.txt'; $handle = fopen($keyword_file, 'r'); if ($handle === false) { continue; } $keyword_list[$cur_dir_name] = fread($handle, 10000); fclose($handle); } // Get the user keywords from the form. if (array_key_exists('keywords', $_POST)) { $user_keywords = $_POST['keywords']; } elseif (array_key_exists('keywords', $_GET)) { $user_keywords = $_GET['keywords']; } else { $user_keywords = ''; } $user_keywords = (string)$user_keywords; // Split on whitespace. $word_array = preg_split( '/\s+/', trim($user_keywords), -1, PREG_SPLIT_NO_EMPTY ); // Set up an array that tracks whether each keyword is ever matched. $word_found = []; foreach ($word_array as $index => $cur_word) { $word_found[$index] = false; } // Display the user's search terms safely. echo 'The following flowers match keywords (' . htmlspecialchars( $user_keywords, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' ) . ')
'; echo ''; echo ''; $cell_count = 0; // Check each species keyword list. foreach ($keyword_list as $flower_dname => $cur_keyword_list) { $all_matched = true; foreach ($word_array as $index => $cur_word) { // stripos() returns false when there is no match. $matched = (stripos($cur_keyword_list, $cur_word) !== false); if (!$matched) { $all_matched = false; } else { $word_found[$index] = true; } } if ($all_matched) { $image_fname = $flower_dname . '/thumb_image.jpg'; // Get the flower name. $handle = fopen($flower_dname . '/name.txt', 'r'); if ($handle === false) { continue; } $flower_name = fread($handle, 256); fclose($handle); /* * Use htmlspecialchars() when putting values into HTML. * Use rawurlencode() when putting a value into a URL. */ $safe_flower_name = htmlspecialchars( $flower_name, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' ); $safe_image_fname = htmlspecialchars( $image_fname, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' ); $safe_flower_dname = rawurlencode($flower_dname); echo ''; $cell_count++; if ($cell_count >= 6) { echo ''; $cell_count = 0; } } } echo '
'; echo ''; echo ''
            . $safe_flower_name
            . ''; echo ''; echo '
' . $safe_flower_name; echo '
'; echo '
'; // Report unmatched keywords. foreach ($word_array as $index => $cur_word) { if ($word_found[$index] === false) { echo 'Keyword ' . htmlspecialchars( $cur_word, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' ) . ' was not found in database
'; } } ?>