常见于DZ论坛,但是zblog也可以实现的,以下内容仅做备忘。
主题include.php里面引入文件“search_config.php”:
search_config.php文件代码:
此文件中引入的文件searchstr.php代码如下:
模板中需要改写一下:
其中主题ID_Cate函数是调用所有分类,如需调用部分分类,可另外设置:
主题include.php里面引入文件“search_config.php”:
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'plugin arch_config.php';并且挂载接口:
Add_Filter_Plugin('Filter_Plugin_Search_Begin','主题ID_SearchMain');
search_config.php文件代码:
<?phprequire dirname(__FILE__) . DIRECTORY_SEPARATOR . 'searchstr.php';function 主题ID_SearchMain() { global $zbp; foreach ($GLOBALS['Filter_Plugin_ViewSearch_Begin'] as $fpname => &$fpsignal) { $fpreturn = $fpname(); if ($fpsignal == PLUGIN_EXITSIGNAL_RETURN) { $fpsignal=PLUGIN_EXITSIGNAL_NONE; return $fpreturn; } } if(!$zbp->CheckRights($GLOBALS['action'])){ Redirect('./'); } $q = trim(htmlspecialchars(GetVars('q','GET'))); $qc = '<b style=\'color:red\'>' . $q . '</b>'; $articles = array(); $category = new Metas; $author = new Metas; $tag = new Metas; $zbp->title = '有关【'.$q.'】的内容:'; $template = $zbp->option['ZC_INDEX_DEFAULT_TEMPLATE']; if($zbp->template->hasTemplate('search')){ $template = 'search';//搜索列表页模版名称 } $w=array(); $w[]=array('=','log_Type','0'); $cid = trim(htmlspecialchars(GetVars('cid','GET')));//传入的分类ID if($cid){ $w[]=array('search','log_CateID',$cid); } if($q){ $w[]=array('search','log_Content','log_Intro','log_Title',$q); }else{ Redirect('./'); } if(!($zbp->CheckRights('ArticleAll')&&$zbp->CheckRights('PageAll'))){ $w[]=array('=','log_Status',0); } if($cid){ $pagebar=new Pagebar('{%host%}search.php?{q='.$q.'}&cid='.$cid.'&{page=%page%}',false); } else { $pagebar=new Pagebar('{%host%}search.php?{q='.$q.'}&{page=%page%}',false); } $pagebar->PageCount=$zbp->displaycount; $pagebar->PageNow=(int)GetVars('page','GET')==0?1:(int)GetVars('page','GET'); $pagebar->PageBarCount=$zbp->pagebarcount; $articles = $zbp->GetArticleList( '*', $w, array('log_PostTime' => 'DESC'), array(($pagebar->PageNow - 1) * $pagebar->PageCount, $pagebar->PageCount), array('pagebar' => $pagebar), null ); if ($articles){ foreach($articles as $article){ $intro = preg_replace('/[\r\n\s]+/', '', trim(主题ID_SubStrStartUTF8(TransferHTML($article->Content,'[nohtml]'),$q,150)) . '...'); $article->Intro = str_ireplace($q,$qc,$intro); $article->Title = str_ireplace($q,$qc,$article->Title); } $zbp->template->SetTags('主题IDSearchSubtitle',''); }else{ $zbp->title = '没有找到有关【'.$q.'】的内容!'; $zbp->template->SetTags('主题IDSearchSubtitle',''); $pagebar=''; foreach($articles as $article){ $article->Intro = preg_replace('/[\r\n\s]+/', '', trim(SubStrUTF8(TransferHTML($article->Intro,'[nohtml]'),150)).'...'); } } $zbp->header .= '<meta name="robots" content="noindex,follow" />' . "\r\n"; $zbp->template->SetTags('title', $zbp->title); $zbp->template->SetTags('articles',$articles); $zbp->template->SetTags('page',1); $zbp->template->SetTags('pagebar',$pagebar); if ($zbp->template->hasTemplate('search')) { $zbp->template->SetTemplate($template); } else { $zbp->template->SetTemplate('index'); } foreach ($GLOBALS['Filter_Plugin_ViewList_Template'] as $fpname => &$fpsignal) { $fpreturn=$fpname($zbp->template); } $zbp->template->Display(); RunTime(); die();}?>
此文件中引入的文件searchstr.php代码如下:
<?php/** * 获取UTF8格式的字符串的子串 * @param string $sourcestr 源字符串 * @param int $startstr 开始字串子串 * @param int $cutlength 子串长度 * @return string */function 主题ID_SubStrStartUTF8($sourcestr,$startstr,$cutlength) { global $zbp; if( function_exists('mb_substr') && function_exists('mb_internal_encoding') && function_exists('mb_stripos') ){ mb_internal_encoding('UTF-8'); return mb_substr($sourcestr, mb_stripos($sourcestr, $startstr), $cutlength); } if( function_exists('iconv_substr') && function_exists('iconv_set_encoding') && function_exists('iconv_strpos') ){ iconv_set_encoding ( "internal_encoding" , "UTF-8" ); iconv_set_encoding ( "output_encoding" , "UTF-8" ); return iconv_substr($sourcestr, iconv_strpos($sourcestr, $startstr), $cutlength); } $returnstr = ''; $i = stripos($sourcestr, $startstr); $n = 0; $str_length = strlen($sourcestr); //字符串的字节数 while (($n < $cutlength) and ($i <= $str_length)) { $temp_str = substr($sourcestr, $i, 1); $ascnum = Ord($temp_str); //得到字符串中第$i位字符的ascii码 if ($ascnum >= 224) //如果ASCII位高与224, { $returnstr = $returnstr . substr($sourcestr, $i, 3); //根据UTF-8编码规范,将3个连续的字符计为单个字符 $i = $i + 3; //实际Byte计为3 $n++; //字串长度计1 } elseif ($ascnum >= 192) //如果ASCII位高与192, { $returnstr = $returnstr . substr($sourcestr, $i, 2); //根据UTF-8编码规范,将2个连续的字符计为单个字符 $i = $i + 2; //实际Byte计为2 $n++; //字串长度计1 } elseif ($ascnum >= 65 && $ascnum <= 90) //如果是大写字母, { $returnstr = $returnstr . substr($sourcestr, $i, 1); $i = $i + 1; //实际的Byte数仍计1个 $n++; //但考虑整体美观,大写字母计成一个高位字符 } else //其他情况下,包括小写字母和半角标点符号, { $returnstr = $returnstr . substr($sourcestr, $i, 1); $i = $i + 1; //实际的Byte数计1个 $n = $n + 0.5; //小写字母和半角标点等与半个高位字符宽... } } if ($str_length > $cutlength) { $returnstr = $returnstr; } return $returnstr;}
模板中需要改写一下:
<form name="search" method="get" action="{$host}search.php"> <input name="q" class="text" id="edtSearch" type="text"> <div class="select"> <select id="cid" name="cid"> {php} $cid =''; echo 主题ID_Cate($cid); {/php} < lect> </div> <input value="" title="搜索" class="button" type="submit"></form>
其中主题ID_Cate函数是调用所有分类,如需调用部分分类,可另外设置:
function 主题ID_Cate($zdycate){ global $zbp; $Catenews=$zbp->GetCategoryList(array('*'),null); $s = ''; foreach ($Catenews as $Catenew) { if($zdycate==$Catenew->ID){ $s .='<option value="'.$Catenew->ID.'" selected="selected" >'.$Catenew->Name.'</option>'; } else { $s .='<option value="'.$Catenew->ID.'">'.$Catenew->Name.'</option>'; } } if($zdycate==""){ $s .='<option value="" selected="selected" >所有分类</option>'; } else { $s .='<option value="">所有分类</option>'; } return $s;}