網站開發(fā)中經常會遇到整站搜索功能,而搜索之后會出現(xiàn)大量的數(shù)據(jù),而通常都是使用分頁的形式去展示這些數(shù)據(jù),當搜索的字段中含有中文時,就可能導致翻頁出現(xiàn)亂碼,導致翻頁失效。搜索使用form表單提交的方式,前端代碼:
formclass=method=getaction={:urlrotue('Search/index')}
pclass=header-form
inputtype=textclass=header-textname=qid=qplaceholder=請輸入搜索關鍵詞
ainputtype=submitclass=header-subvalue=/a
/
/form
下面是頁碼出現(xiàn)亂碼的地址,點擊后無法跳轉到第二頁的內容:
查看了ThinkPHP\Library\Think\Page.class文件后發(fā)現(xiàn)代碼是這樣的
然后只需要這樣修改:
privatefunctionurl($page){
returnstr_replace(urlencode('[PAGE]'),$page,$this-url);
}
$request_url=$_SERVER[REQUEST_URI];
if(!preg_match(/\/p\/\d+/,$request_url)){
$request_url=str_replace(.html,'/p/'.urlencode('[PAGE]').'.html',$request_url);
}
$this-url=preg_replace(/\/p\/\d+\.html/,'/p/'.urlencode('[PAGE]').'.html',$request_url);
得到的正常的地址應該是這樣的:
在后續(xù)的使用過程中又發(fā)現(xiàn),URL在Apache上是/不會有問題,但是在IIS上用/會亂碼,必須用?=這種格式才行或者到需要通過url?=傳遞參數(shù)時。
$this-parameter[$this-p]='[PAGE]';
$paramStr=;
foreach($this-parameteras$key=$value){
$paramStr=$paramStr.''.$key.'='.$value;
}
$paramStr=substr($paramStr,1,strlen($paramStr));
$this-url=U(ACTION_NAME).(strpos(U(),?)?'':'?').$paramStr;
得到的地址是這樣的: