
複数のテキストファイルから単語を検索するプログラム。
横断検索的な事がしたい。
トップ絵はAIに描いてもらいました。
検索フォーム
<html>
<head>
<meta charset="UTF-8">
<title>横断検索</title>
</head>
<body>
<h1>横断検索</h1>
<form method="post" action="">
<input type="text" name="search_word">
<input type="submit" value="検索">
</form>
</body>
</html>
検索ワードを取得
<?php
if (isset($_POST['search_word'])) {
$search_word = $_POST['search_word'];
// $search_word を使って検索処理を行う
}
?>
検索対象のファイルを取得
<?php
if (isset($_POST['search_word'])) {
$search_word = $_POST['search_word'];
$dir = './files/'; // 検索対象のディレクトリ
$files = glob($dir . '*'); // ディレクトリ内のファイルを取得
}
?>
検索を実行
<?php
if (isset($_POST['search_word'])) {
$search_word = $_POST['search_word'];
$dir = './files/'; // 検索対象のディレクトリ
$files = glob($dir . '*'); // ディレクトリ内のファイルを取得
foreach ($files as $file) {
$content = file_get_contents($file); // ファイルを読み込む
if (strpos($content, $search_word) !== false) {
echo "<p>{$file}</p>"; // 検索ワードが含まれている場合はファイル名を表示する
}
}
}
?>
ファイルの内容を表示
<html> <head> <meta charset="UTF-8"> <title>ファイルの内容</title> </head> <body> <h1>ファイルの内容</h1> <div><?php echo nl2br(file_get_contents($_GET['file'])); ?></div> </body> </html>
ファイル名をクリックした時に内容を表示する
<?php
if (isset($_POST['search_word'])) {
$search_word = $_POST['search_word'];
$dir = './files/'; // 検索対象のディレクトリ
$files = glob($dir . '*'); // ディレクトリ内のファイルを取得
foreach ($files as $file) {
$content = file_get_contents($file); // ファイルを読み込む
if (strpos($content, $search_word) !== false) {
echo "<p><a href=\"file.php?file={$file}\">{$file}</a></p>"; // ファイル名をリンクにして表示する
}
}
}
?>


コメント