我对PHP非常陌生。请帮忙:)
我只是希望从搜索栏中输入的字符串能够提取一个.html文件,文件名中包含该字符串。
我基本上每个.html页面都会显示一本书的列表,其中每个.html页面都是按作者(即freud.html)分类的。因此,当我通过搜索栏搜索“ freud”书籍时,我希望显示freud.html。
我不确定如何将搜索栏字符串指向php文件。另外,我不确定搜索到的字符串如何提取所需的.html文件或仅显示错误页面。
这是我的搜索栏:
<div class="search-container">
<form action="searching.php">
<input type="text" placeholder="Search Author..." name="search">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</div>
这是我在网上找到的PHP页面:
<?php
// string to search in a filename.
$searchString = 'myFile';
// all files in my/dir with the extension
// .html
$files = glob('/*.html');
// array populated with files found
// containing the search string.
$filesFound = array();
// iterate through the files and determine
// if the filename contains the search string.
foreach($files as $file) {
$name = pathinfo($file, PATHINFO_FILENAME);
// determines if the search string is in the filename.
if(strpos(strtolower($name), strtolower($searchString))) {
$filesFound[] = $file;
}
}
// output the results.
print_r($filesFound);
?>
答案 0 :(得分:1)
当表单:
<form action="searching.php">
<input type="text" placeholder="Search Author..." name="search">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
提交后,GET请求将通过路径/searching.php
发送到您的Web服务器,并且GET参数search
=您的搜索输入。然后,您的网络服务器会加载PHP文件searching.php
并执行它。
在PHP中,您可以使用$_GET全局数组访问GET参数。
因此,在您的PHP中,您可以使用$_GET["search"]
访问搜索字符串。
对于第二个问题,找到HTML文件后,可以使用readfile显示它。如果找不到文件,则可以加载其他文件,例如error.html
。