我刚刚开始学习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);
}
?>
我的搜索内容是在12.html文件中。如果我在搜索框中输入单词,我的页面的整个主体就会出现,因为我需要特定的行或其中的单词。即使我输入一个不在我的内容中的单词。我的文件正在显示我不知道我在哪里犯了错误,任何人都可以引导我。
答案 0 :(得分:0)
首先,HTML语法中存在错误。您希望操作指向您的脚本。
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
输入项目中存在错误。
<input type="text" name="keyword" id="keyword" width="50" value="" />
<?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 " . count($matches) . " matches:<br />";
//here you are imploding the entire array;
echo implode("<br />", $matches);
}
else{
echo "No matches found";
fclose ($file);
}
?>
我不明白你为什么要破坏这些比赛,因为这只会列出相同的单词,但却多次被发现。如果你想找到特定的行,你可以试试这个。
<?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):
?>