需要帮助在Php中创建搜索框吗

需要帮助在Php中创建搜索框吗,php,html,search-box,Php,Html,Search Box,我以前刚开始学习php。。我正在做一个搜索框,但是我不能得到结果 <html> <head><title>Search Form</title></head> <body> <form action="12.html" method="GET"> <input type="text" name="keyword" id="keyword width=

我以前刚开始学习php。。我正在做一个搜索框,但是我不能得到结果

  <html>
  <head><title>Search Form</title></head>
  <body>
        <form action="12.html" method="GET">
               <input type="text" name="keyword" id="keyword width="50" value="" />
               <input type="submit" value="Search"/>
  </form>
  </body>
  </html>
  <?php
  $searchfor = $_GET['keyword'];
  $file = '12.html';
  $contents = file_get_contents($file);
  $pattern = preg_quote($searchfor, '/');
  $pattern = "/^.*$pattern.*\$/m";
  if(preg_match_all($pattern, $contents, $matches)){
  echo "Found matches:<br />";
  echo implode("<br />", $matches[0]);
  }
  else{
  echo "No matches found";
  fclose ($file); 
  }
  ?>

搜索表

首先,HTML语法中存在错误。您希望操作指向脚本

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">

我不明白你为什么要对匹配项进行内爆,因为无论找到多少次,它都只会列出同一个单词。如果你想找到具体的路线,你可以试试这个

<?php
$lines = explode('\n',$contents);
$lineNum = 1;
$linesFound = array();
foreach ($lines as $line){
    if (preg_match($pattern, $line)){
        $linesFound[] = $lineNum;
    }
    $lineNum++
}
if (!empty($linesFound)){
}
echo "Keyword found on line(s): 
?>

您的表单将转到12.html,而实际上它将转到当前的PHP页面。将操作更改为“whateverthispageiscalled.php”,然后进行测试?它无法重定向到我的12.html文件本身,无法显示我正在搜索的关键字
<?php
$lines = explode('\n',$contents);
$lineNum = 1;
$linesFound = array();
foreach ($lines as $line){
    if (preg_match($pattern, $line)){
        $linesFound[] = $lineNum;
    }
    $lineNum++
}
if (!empty($linesFound)){
}
echo "Keyword found on line(s): 
?>