【问题标题】:Search bar for HTML files in a folder or from links in XML file文件夹中的 HTML 文件或 XML 文件中的链接的搜索栏
【发布时间】: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 我又更新了

标签: php xml html


【解决方案1】:

也许你可以使用PHP的scandir函数

<?php
$dir    = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);

print_r($files1);
print_r($files2);
?>

上面的例子会输出类似于:

Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)
Array
(
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
)

来源:http://php.net/manual/en/function.scandir.php

警告!!!当您使用动态用户输入时,此示例可能容易受到路径遍历的影响。在继续执行此脚本之前,请检查、检查并仔细检查输入。

【讨论】:

    【解决方案2】:

    您可以将opendirreaddir 用于此类目的。

    <?php
    $dir = "/your_folder_here/";
    
    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                if($file == $_POST['SEARCHBOX_INPUT']){
                    echo('<a href="'.$dir . $file.'">'. $file .'</a>'."\n");
                }
            }
            closedir($dh);
        }
    }
    ?>
    

    记得更改文件路径。还将中间的$_POST 命令更改为搜索框输入的名称。这将找到与您的搜索完全匹配的内容。可以进行更改以找到更接近的匹配项以获得更好的用户体验。

    【讨论】:

      【解决方案3】:

      你可能想尝试一个简单的表单,它会触发一个 php 函数 这是基本的,添加安全层很重要(防止任何搜索运行等)

      <?php
      if (isset($_POST['search'])) {
       $dir = array();
       $dir = scandir('/tmp');
       $result = array_search($_POST['search'],$dir);
      }
      
      $output =<<<OUT
      <html>
      <form action="test.php" type="POST">
      <input name="search" type="text" />
      </form>
      <p>result: {$result}</p>
      </html>
      OUT;
      
      echo $output;
      ?>
      

      【讨论】:

        猜你喜欢
        • 2018-02-26
        • 2013-12-27
        • 1970-01-01
        • 1970-01-01
        • 2018-07-31
        • 2021-12-23
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        相关资源
        最近更新 更多