<?php
// Define the directory to search in
$directory = './';

// Define the maximum number of results to display
$max_results = 27;

// Define the phrase to search for
$phrase_to_search = '';

// Create a form to input the phrase
?>
<form action="" method="post">
    <input type="text" name="phrase_to_search" placeholder="खोज के लिए शब्द दर्ज करें...">
    <input type="submit" value="खोज">
</form>

<?php
// Check if the form has been submitted
if (isset($_POST['phrase_to_search'])) {
    $phrase_to_search = $_POST['phrase_to_search'];

    // Set the encoding to UTF-8
    header('Content-Type: text/html; charset=UTF-8');

    // Search for the phrase in HTML files
    $found_files = array();
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
    foreach ($iterator as $file) {
        if ($file->isFile() && $file->getExtension() == 'html') {
            $file_path = $file->getPathname();
            $html_content = file_get_contents($file_path);

            // Extract the text from <p>, <div>, and <ul> tags
$text = '';
preg_match_all('/<p>(.*?)<\/p>/', $html_content, $p_text);
preg_match_all('/<div>(.*?)<\/div>/', $html_content, $div_text);
preg_match_all('/<ul>(.*?)<\/ul>/', $html_content, $ul_text);

$text.= implode('', $p_text[1]);
$text.= implode('', $div_text[1]);
$text.= implode('', $ul_text[1]);

            if (strpos($text, $phrase_to_search)!== false) {
                // Extract the title of the HTML file
                $title = '';
                $lines = explode("\n", $html_content);
                foreach ($lines as $line) {
                    if (strpos($line, '<title>')!== false) {
                        $title = substr($line, strpos($line, '<title>') + 7);
                        $title = substr($title, 0, strpos($title, '</title>'));
                        break;
                    }
                }
                if (empty($title)) {
                    $title = $file->getFilename();
                }

                // Extract a snippet of the exact phrase found
                $start_index = strpos($text, $phrase_to_search);
                if ($start_index === false) {
                    continue;
                }
                $snippet_start = $start_index;
                $snippet_end = $start_index + strlen($phrase_to_search);
                $snippet = substr($text, $snippet_start, 300);
                $snippet = strip_tags($snippet); // remove HTML tags
                if (strlen($snippet) > 200) {
                    $last_space_index = strrpos($snippet,' ');
                    if ($last_space_index!== false) {
                        $snippet = substr($snippet, 0, $last_space_index). '.....';
                    } else {
                        $snippet = substr($snippet, 0, 200). '.....';
                    }
                }

                // Highlight the found phrase
                $snippet = str_replace($phrase_to_search, "<span style='background-color: yellow; font-weight: bold;'>$phrase_to_search</span>", $snippet);

                // Add the title, link, and snippet to the results array
                $found_files[] = array(
                    'title' => $title,
                    'link' => $file_path,
                   'snippet' => $snippet
                );

                // Check if the maximum number of results has been reached
                if (count($found_files) >= $max_results) {
                    break;
                }
            }
        }
    }

    // Display the results
    if (!empty($found_files)) {
        echo "खोज शब्द में पाए गए निम्नलिखित फाइलें";
        foreach ($found_files as $file) {
            echo "<li><a href='{$file['link']}' target='_blank'>{$file['title']}</a> - {$file['snippet']}...</li>";
        }
        if (count($found_files) == $max_results) {
            echo " ";
        }
    } else {
        echo "खोज शब्द में कोई फाइल नहीं मिली।";
    }
}
?>
