復(fù)制代碼 代碼如下:
<?php
$keyword="asp php,jsp";
$keyword=str_replace(" "," ",$keyword);
$keyword=str_replace(" ",",",$keyword);
$keyarr=explode(',',$keyword);
for($index=0;$index<count($keyarr);$index++)
{
$whereSql .= " And (arc.title like '%$keyarr[$index]%' Or arc.keywords like '%$keyarr[$index]%') ";
}
echo $whereSql;
為了同時(shí)支持空格與逗號(hào),需要提前替換為統(tǒng)一的逗號(hào),就是先將空格都替換為逗號(hào),然后再通過逗號(hào)分割字符串,然后循環(huán)拼接sql查詢語句。
str_replace就是php中常用的字符串替換函數(shù)。
explode就是php中常用的字符串分割為數(shù)組的函數(shù)。