주제: 검색 키워드문제
안녕하세요...
텍스트큐브 1.7에는 기사검색창이 있습니다.
그 검색창에 "홍길동 영화"라고 입력하고 검색을 하면 기사제목과 기사본문에서 "홍길동 영화"가 들어간것만 찾아냅니다.
"홍길동"도 들어가고 "영화"도 들어간 기사는 찾지 못하죠.
나름대로 소스를 분석해보고 고쳐보았습니다.
lib\model\blog.entry.php파일이예요.
우선. 파일에 다음의 두개 함수를 추가하고요..
function GetFractionWord($text)
{
$ret = array();
if (strlen($text) == 0)
return $ret;
$start = 0;
$cur = -1;
$idx = 0;
$idx1= 0;
$idx2 = 0;
$text = str_replace("\\\\", "\\", $text);
$text = str_replace("\\\"", "\"", $text);
$text = str_replace("\\'", "'", $text);
$text = " " . $text . " ";
while($start < strlen($text) - 1)
{
$idx = strpos($text, " \"", $start);
if ($idx === false)
{
AddArrayStringItem($ret, substr($text, $start));
break;
}
else
{
// pre text
if ($idx > $start)
AddArrayStringItem($ret, substr($text, $start, $idx - $start + 1));
$start = $idx;
$idx = strpos($text, "\" ", $start + 3);
if ($idx === false)
{
AddArrayStringItem($ret, substr($text, $start));
break;
}
else
{
$ret[] = substr($text, $start + 2, $idx - ($start + 2));
$start = $idx + 2;
}
}
}
return $ret;
}
function AddArrayStringItem(&$arrlist, $text)
{
$n = strpos($text, " ");
while(!($n === false))
{
if ($n > 0)
$arrlist[] = substr($text, 0, $n);
$text = substr($text, $n+1);
$n = strpos($text, " ");
}
if (strlen($text) > 0)
$arrlist[] = $text;
}
다음으로. getEntryListWithPagingBySearch함수와 getEntriesWithPagingBySearch함수에 있는 SQL검색조건을 다음과 같이 변경시킵니다.
원래소스:
$cond = strlen($search) == 0 ? 'AND 0' : "AND (e.title LIKE '%$search%' OR e.content LIKE '%$search%')";
수정한 소스:
$arrSearch = GetFractionWord($search);
if (count($arrSearch) == 0)
{
$cond = 'AND 0';
}
else
{
$cond = '';
foreach ($arrSearch as $s)
{
$cond .= " AND (e.title LIKE '%$s%' OR e.content LIKE '%$s%')";
}
}
우와 같이 수정하면 검색창에
홍길동 영화 "멋있는 사람" 싸이트
라고 입력하면
홍길동, 영화, 멋있는 사람, 싸이트
가 들어있는 모든 기사가 검색됩니다.
공백으로 구분만 해주면 되니 정말 쓸모있겠죠.
텍스트큐브의 다음 버젼에서 갱신되기를 바라면서...
화이팅!!!