根据用户搜索的关键字通过php读取txt文件?

时间:2013-08-11 23:45:44

标签: php

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

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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)。否则,使用子匹配时可能无法获得正确的输出。