【发布时间】:2011-12-08 14:29:28
【问题描述】:
我一直在为网站上某些类型的帖子构建一个 PHP 搜索工具(为此,请接受 mySQL 是不可能的)。
经过一系列的过程,我们得到了每个帖子的标题和标签,并将它们存储在一个名为$full的变量中。
搜索词位于一个名为$terms的变量中
$full = $title . ' ' . $tago[$result->ID];
两者都转换为小写。
然后我们想使用$terms在$full中寻找相似的词
我试过了。
$final = strpos($full,$terms);
它有效,但不如我需要的那么好。
- 这将匹配标题和标签中的相似词,但根本不处理空格。我尝试从标题和标签中删除空格和逗号,但无济于事。
- 如果用户输入的某人的名字由两个标签而不是一个标签组成,它将找不到任何结果。
- 它不能处理一个以上的单词,更不用说一个以上的术语了,我希望它能够做到这两点。
如果有帮助,这里是完整的脚本
$proto = $_GET['p'];
$terms = $_GET['s'];
$terms = strtolower($terms);
$terms = str_replace(' ', '', $terms);
$ids = array();
if($proto == 'inline') {
$search = get_posts('post_type=post&post_status=publish');
foreach($search as $result) {
$title = get_the_title($result);
$tags = wp_get_post_tags( $result->ID);
foreach($tags as $tag){ $tago[$result->ID].= $tag->name;}
$full = $title . ' ' . $tago[$result->ID];
$full = strtolower($full);
$final = strpos($full,$terms);
if($final != false){
$ids[] = $result->ID;
}
}
if ($ids[0] == '') {
echo '<div align="center" style="text-align:center; color:#FFF;">No Results Found</div>';
return false; } else {
$args = array( 'post__in' => $ids );
$srs = get_posts($args);
foreach($srs as $sr) {
echo '<a href="'.$sr->post_slug.'"><img src=""/><b>'.$sr->post_title.'</b>'. $tago[$result->ID].'<span>'.date('dS M Y', strtotime($sr->post_date)).'</span></a>';
}
}
}
价值观
$terms 可能包含用户输入的一些值以进行搜索,例如“红色汽车”;
$full 包含帖子标题和标签,所以它可能会说。 '红色的vaxhaul不是很好,车,车,可怕,丑陋'
所以在这种情况下应该可以找到。
【问题讨论】:
-
您能否举个例子说明您希望它找到什么,显示 $full 和 $terms 的值可能会更容易为您创建解决方案