问题描述
我最近在我的网站上添加了 Sphider 搜寻器,以添加搜索功能.但是我下载的Sphider发行版附带的默认search.php太简单了,无法与我网站的其余部分很好地集成.我在网站顶部有一个小的导航栏,其中有一个搜索框,我希望能够使用Ajax通过该搜索字段访问Sphider的搜索结果.为此,我认为我需要让Sphider以JSON格式返回其结果.
I've recently added the Sphider crawler to my site in order to add search functionality. But the default search.php that comes with the distribution of Sphider that I downloaded is too plain and doesn't integrate well with the rest of my site. I have a little navigation bar at the top of the site which has a search box in it, and I'd like to be able to access Sphider's search results through that search field using Ajax. To do this, I figure I need to get Sphider to return its results in JSON format.
我这样做的方式是我使用了一个输出JSON的主题"(Sphider支持其输出主题化").我在Sphider网站上的此线程上找到了该主题.它似乎可以正常工作,但是更严格的JSON解析器不会解析它.这是一些JSON输出示例:
The way I did that is I used a "theme" that outputs JSON (Sphider supposts "theming" its output). I found that theme on this thread on Sphider's site. It seems to work, but more strict JSON parsers will not parse it. Here's some example JSON output:
{"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:
The issue is that there is a trailing comma near the end. According to this, "trailing commas are not allowed" when using PHP's json_decode()
function. This JSON also failed to parse using this online formatter. But when I took the comma out, it worked and I got this better-formatted 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解决方案对我不起作用.
Now, how would I do this programmatically? And (perhaps more importantly), is there a more elegant way of accomplishing this? And you should know that PHP is the only language I can run on my shared hosting account, so a Java solution for example would not work for me.
推荐答案
在search_result.html
中,可以将forc循环末尾的,
包围起来,条件是仅在索引严格小于数字时才打印页数-1.
In search_result.html
, you can surround the ,
at the end of the foreach loop with condition to only print if the index is strictly less than the number of pages - 1.
这篇关于使Sphider输出JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!