我最近将Sphider爬虫添加到我的网站以添加搜索功能。但是我下载的Sphider发行版附带的默认搜索.php过于简单,不能与我网站的其余部分很好地集成。我在网站顶部有一个小导航栏,里面有一个搜索框,我希望能够使用 Ajax 通过该搜索字段访问 Sphider 的搜索结果。为此,我想我需要让 Sphider 以 JSON 格式返回其结果。
我这样做的方式是我使用输出JSON的"主题"(Sphider支持"主题化"其输出)。我在Sphider的网站上找到了这个主题。它似乎有效,但更严格的 JSON 解析器不会解析它。下面是一些示例 JSON 输出:
{"result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ", "results":[ { "idented":"false", "num":"1", "weight":"[100.00%]", "link":"http://www.avtainsys.com/articles/Triple_Contraints", "title":"Triple Contraints", "description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or", "link2":"http://www.avtainsys.com/articles/Triple_Contraints", "size":"3.3kb" }, { "num":"-1" } ], "other_pages":[ { "title":"1", "link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=", "active":"true" }, ] }
问题是末尾附近有一个尾随逗号。据此,使用 PHP 的 json_decode()
函数时"不允许尾随逗号"。此 JSON 也未能使用此在线格式化程序进行解析。但是当我去掉逗号时,它起作用了,我得到了这个格式更好的 JSON:
{
"result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ",
"results":[
{
"idented":"false",
"num":"1",
"weight":"[100.00%]",
"link":"http://www.avtainsys.com/articles/Triple_Contraints",
"title":"Triple Contraints",
"description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or",
"link2":"http://www.avtainsys.com/articles/Triple_Contraints",
"size":"3.3kb"
},
{
"num":"-1"
}
],
"other_pages":[
{
"title":"1",
"link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=",
"active":"true"
}
]
}
现在,我将如何以编程方式执行此操作?而且(也许更重要的是),有没有更优雅的方式来实现这一目标?而且您应该知道PHP是我可以在共享主机帐户上运行的唯一语言,因此例如Java解决方案对我不起作用。
在 search_result.html
中,您可以在 foreach 循环的末尾将,
括起来,条件是仅在索引严格小于页数 - 1 时才打印。