我是一名自学成才的PHP爱好者。 我得到以下代码来执行搜索。 函数stringToSearch清除所有“葡萄牙语”危险字符,函数jm_search执行搜索...一切顺利,搜索完成没有任何问题。
现在我想执行全文搜索,我已经调整了第一个代码来执行此操作但不知何故它不起作用...总是返回0结果。 到目前为止,我一直在努力,但没有成功。
第一段代码
function stringToSearch($str) {
//Let’s transform string to lowercase
$str = trim(strtolower($str));
//Taking out extra spaces from string... “long word” or “long word” becomes “long word”
while ( strpos($str," ") )
$str = str_replace(" "," ",$str);
//Change de dangerous characters "ã,á..." by clean characters "a"
$caracteresDanger = array ("Ã","ã","Õ","õ","á","Á","é","É","í","Í","ó","Ó","ú","Ú","ç","Ç","à","À","è","È","ì","Ì","ò","Ò","ù","Ù","ä","Ä","ë","Ë","ï","Ï","ö","Ö","ü","Ü","Â","Ê","Î","Ô","Û","â","ê","î","ô","û","!","?");
$caracteresClean = array ("a","a","o","o","a","a","e","e","i","i","o","o","u","u","c","c","a","a","e","e","i","i","o","o","u","u","a","a","e","e","i","i","o","o","u","u","A","E","I","O","U","a","e","i","o","u",".",".",".",".",".",".","." ,"." ,".");
$str = str_replace($caracteresDanger,$caracteresClean,$str);
//Now we don’t have any danger character in the string, and all are "lower",
//Let’s build the Regular Expression to MySQL
$caractresSimple = array("a","e","i","o","u","c");
$caractresEnvelope = array("[a]","[e]","[i]","[o]","[u]","[c]");
$str = str_replace($caractresSimple,$caractresEnvelope,$str);
$caracteresForRegExp = array("(a|ã|á|à|ä|â|ã|á|à|ä|â|Ã|Á|À|Ä|Â|Ã|Á|À|Ä|Â)",
"(e|é|è|ë|ê|é|è|ë|ê|É|È|Ë|Ê|É|È|Ë|Ê)",
"(i|í|ì|ï|î|í|ì|ï|î|Í|Ì|Ï|Î|Í|Ì|Ï|Î)",
"(o|õ|ó|ò|ö|ô|õ|ó|ò|ö|ô|Õ|Ó|Ò|Ö|Ô|Õ|Ó|Ò|Ö|Ô)",
"(u|ú|ù|ü|û|ú|ù|ü|û|Ú|Ù|Ü|Û|Ú|Ù|Ü|Û)",
"(c|ç|Ç|ç|Ç)" );
$str = str_replace($caractresEnvelope,$caracteresForRegExp,$str);
//Changing spaces by .*
$str = str_replace(" ",".*",$str);
//Return final String!
return $str;
}
function jm_search(){
global $database, $sstring, $scategory, $my, $cinConfig, $Itemid, $option, $mainframe;
$scategory = 'description';
$str=stringToSearch($sstring);
$searchTerms = $scategory.” REGEXP \"" . $str . "\"";
$query = "SELECT * FROM #__jmovies WHERE ".$searchTerms." AND published = 1 AND access <= ".(int)$my->gid." ORDER BY title”;
$database->setQuery($query);
//echo $database->getQuery();
$rows = $database->loadObjectList();
$total = count($rows);
}
第二段代码(全文检索)
function jm_search(){
global $database, $sstring, $scategory, $my, $cinConfig, $Itemid, $option, $mainframe;
$scategory = 'description'
$str=stringToSearch($sstring);
$searchArray = explode(" ", $str);
$query="SELECT * FROM #__jmovies WHERE MATCH (`description`) AGAINST ('";
$i=0;
foreach ($searchArray as $word) {
$query .= "+".$word."* ";
}
$query .= "' IN BOOLEAN MODE) AND published = 1 AND access <= ".(int)$my->gid." ORDER BY title";
$database->setQuery($query);
//echo $database->getQuery();
$rows = $database->loadObjectList();
$total = count($rows);
}