【发布时间】:2018-10-08 12:14:48
【问题描述】:
我有一个包含大约 2900 个 HTML 文件的文件夹。
我想在我的主页上有一个搜索栏,我可以用它来搜索这个文件夹,以便轻松查找和查看您搜索的 html 文件。
我目前正在尝试将链接放在 xml 文件中并搜索它们。
我现在有这段代码,但它非常慢。有什么建议可以让它更实用吗?
这是我的 index.php 文件:
<html>
<head>
<script>
function showResult(str) {
if (str.length==0) {
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("livesearch").innerHTML=this.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</body>
</html>
我的 search.php 文件:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("table_of_content.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
这就是 XML 文档的样子。我在这里只添加了一个链接,但在 XML 文档中总共有大约 2900 个这样的链接:
<pages>
<link>
<title>TITLE</title>
<url>URL</url>
</link>
</pages>
【问题讨论】:
-
欢迎来到stackoverflow!你试过什么了?一些建议:使用
scandir()函数和glob()函数 -
StackOverflow 不是代码编写服务。我们总是乐于帮助和支持新的编码员,但您首先需要帮助自己。您应该尝试自己编写代码。请阅读如何创建Minimal, Complete, and Verifiable example. 和Jon Skeet's SO Checklist
-
用我尝试基于此搜索的代码编辑了我的帖子
-
@Sanguinary 我又更新了