如何使用PHP将文本文件搜索结果以表格形式展示
Search Results Table for Text File Keyword Search
Hey there! I've adjusted your code to display the search results in a neat HTML table with two clear columns: Filename and Line Number. This makes the results much easier to scan at a glance. Here's the revised code with explanations of the key changes:
Key Changes Made:
- Result Collection: Instead of echoing results as we read each file, we first collect all matches into an array. This lets us build a clean table once all files are processed.
- Avoid Duplicate Line Entries: If a line has multiple instances of the keyword, we only record the line number once (no more repeated line numbers for the same line).
- Table Layout: Added a styled HTML table to present results clearly, with headers for Filename and Line Number.
- Empty Result Handling: Added a message to let the user know if no matches were found.
- Cleaner Output: Removed redundant echo statements and streamlined the file reading logic.
Revised Full Code
<?php $results = []; if(isset($_POST["search"])) { $search = trim($_POST['name']); // Only proceed if search term isn't empty if(!empty($search)) { echo "<h2>Search results for: <em>$search</em></h2>"; foreach(glob($_SERVER['DOCUMENT_ROOT']."./readfiletest/*.txt") as $txts) { $line = 1; $myFileLink = fopen($txts, 'r'); while(!feof($myFileLink)) { $myFileContents = fgets($myFileLink); // Check if the keyword exists in the line (case-insensitive) if(preg_match('/'.preg_quote($search,'/').'/i', $myFileContents)) { // Add to results array (avoid duplicates for same line/file) $results[] = [ 'filename' => basename($txts), 'line' => $line ]; } ++$line; } fclose($myFileLink); } } else { echo "<p>Please enter a search term!</p>"; } } ?> <html> <head> <meta http-equiv="Content-Language" content="ar-sa"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style> #form { background: -webkit-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px); background: -moz-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px); background: linear-gradient(bottom, #CCCCCC, #EEEEEE 175px); margin: auto; width: 300px; height: 200px; position: absolute; font-family: Tahoma, Geneva, sans-serif; font-size: 14px; font-style: italic; line-height: 24px; font-weight: bold; color: #09C; text-decoration: none; border-radius: 10px; padding: 10px; border: 1px solid #999; border: inset 1px solid #333; -webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3); -moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3); box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3); } .results-table { margin: 250px auto 0; border-collapse: collapse; width: 80%; font-family: Arial, sans-serif; } .results-table th, .results-table td { border: 1px solid #ddd; padding: 12px; text-align: left; } .results-table th { background-color: #f2f2f2; font-weight: bold; color: #333; } .results-table tr:hover { background-color: #f5f5f5; } .no-results { text-align: center; margin-top: 250px; font-family: Arial, sans-serif; color: #666; } </style> </head> <body> <div id="form"> <form action="index.php" method="post"> <h1 align="center">Search Form</h1> <p>Enter your string: <input type="text" id="idName" name="name" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; ?>" /></p> <p align="center"><input type="Submit" name="search" value="Search" /></p> </form> </div> <?php if(isset($_POST["search"]) && !empty($search)): ?> <?php if(!empty($results)): ?> <table class="results-table"> <thead> <tr> <th>Filename</th> <th>Line Number</th> </tr> </thead> <tbody> <?php foreach($results as $result): ?> <tr> <td><?php echo htmlspecialchars($result['filename']); ?></td> <td><?php echo $result['line']; ?></td> </tr> <?php endforeach; ?> </tbody> </table> <?php else: ?> <p class="no-results">No matches found for "<em><?php echo htmlspecialchars($search); ?></em>".</p> <?php endif; ?> <?php endif; ?> </body> </html>
Additional Notes:
- Added
htmlspecialchars()to output to prevent XSS vulnerabilities (always a good practice when displaying user input). - Styled the table with hover effects and clean borders for better readability.
- Moved the results display below the form so it doesn't overlap.
- Added a check to prevent empty search queries from running.
内容的提问来源于stack exchange,提问作者Rany Fahed
相关产品推荐
相关产品推荐

