-1

我需要知道如何通过 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>

任何帮助,将不胜感激。

4

2 回答 2

0

您需要稍微调整一下,无需在输入中添加值。当您发布页面时,值会被提交,因此请删除value="<?php $searchfor ?>"

脚本应该是这样的:

<?php
    if(!empty($_POST['something'])) {
    $file = 'form.txt';
    $searchfor = '';
    // the following line prevents the browser from parsing this as HTML.
    header('Content-Type: text/plain');
    $searchfor = $_POST['something'];
    $contents = file_get_contents($file);
    $pattern = preg_quote($searchfor, '/');
    $pattern = "/^.*$pattern.*\$/m";
    if(preg_match_all($pattern, $contents, $matches)){
       echo "Found matches:\n";
       echo implode("\n", $matches[0]);
    }
    else{
       echo "No matches found";
    }
    header('Content-Type: text/html');
    }
?>

  <form method="post" action="">
    <input type="text" name="something" />
    <input type="submit" name="submit" />
  </form>
于 2013-08-12T00:14:03.090 回答
0
<?php
$file = 'somefile.txt';
$searchfor = '';

if (!empty($_POST['something'])) {
    // 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 = $_POST['something'];
    // 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";
       var_dump($matches); 
    }
    else{
       echo "No matches found";
    }
    exit; // since it is parsed as plaintext, you can't use the html form below anyway
}

?>

  <form method="post" action="">
    <input type="text" name="something" value="<?php $searchfor ?>" />
    <input type="submit" name="submit" />
  </form>

事情发生了变化:

  1. 在搜索之前检查是否设置了 $_POST['something']。
  2. 删除$pattern = preg_quote($searchfor, '/');,因为我总是有双斜线。不过,您可能会重新检查。
  3. 实际设置$pattern$pattern = preg_quote($searchfor, '/');,否则您将永远无法获得用户输入。
  4. 退出脚本(见评论)。
  5. 请注意,我使用了var_dump($matches). 否则,当使用子匹配时,您可能无法获得正确的输出。
于 2013-08-12T00:23:34.483 回答