我需要知道如何通过 PHP 读取 txt 文件并根据用户通过文本字段搜索的内容显示结果?
我找到了这个 PHP 代码,但我不知道如何在其中实现一个文本框。
<?php
$file = 'somefile.txt';
$searchfor = '';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
?>
我确实尝试过这样的事情,但没有奏效:
<form method="post" action="">
<input type="text" name="something" value="<?php $searchfor ?>" />
<input type="submit" name="submit" />
</form>
任何帮助,将不胜感激。