我一直在为网站上某些类型的帖子构建一个 PHP 搜索工具(为此,请接受 mySQL 是不可能的)。
经过一系列程序,我们得到了每个帖子的标题和标签,并将它们存储在一个名为$full
.
搜索词位于一个名为的变量中$terms
$full = $title . ' ' . $tago[$result->ID];
两者都转换为小写。
$full
然后我们想在使用中寻找相似的词$terms
我试过这个。
$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 不是很好,车辆,汽车,可怕,丑陋”
所以应该在这种情况下找到。