我一直在建立对某些类型的网站上发布了一个关于PHP搜索工具(本作的目的,请接受MySQL是不可能的)。PHP搜索关键字
经过一系列的程序,我们获得了冠军,并为每个岗位的标签并将其存储在一个名为$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>';
}
}
}
值
$条款可能包含某些值由用户输入的搜索说,“红车” ;
$完全包含文章标题和标签,因此可能会说。 “红色vaxhaul是不是很好,车辆,汽车,可怕,丑陋”
所以应该在这种情况下被发现。
你可以举一个你想要它找到的例子,显示$ full和$ terms的值可能会更容易为你创建一个解决方案 –
@Jason完成更新 –