본문 바로가기

Web/PHP

논리검색

<?php
// +----------------------------------------------------------------------+
// | 2004-05-31 | 논리 검색 하기 |
// +----------------------------------------------------------------------+
// | Authors: aucd29 <aucd29@kornet.net>                                 |
// +----------------------------------------------------------------------+

// array:필드 string:검색어
function logicSearch(&$aField,$search)
{
    $search = trim($search);    
    $aWord = explode(" ",$search);
    $nField = count($aField)-1;
    unset($que);

    for($i=0; $i<count($aWord); $i++)
    {
        if(strToUpper($aWord[$i])=='AND' or strToUpper($aWord[$i])=='OR' or strToUpper($aWord[$i])=='NOT')
        {
            if(strToUpper($aWord[$i])=='NOT')
            {
                $i++;
                $que .= " AND (";
                foreach($aField as $index => $value)
                {
                    $que .= " $value NOT LIKE '%".$aWord[$i]."%'";
                    if($index != $nField) $que .= " AND ";
                }
                $que .= ")";
            }
            else
            {
                if(strToUpper($aWord[$i])=='AND')
                {
                    $i++;
                    $que .= " AND (";
                    foreach($aField as $index => $value)
                    {
                        $que .= " $value LIKE '%".$aWord[$i]."%'";
                        if($index != $nField) $que .= " OR ";
                    }
                    $que .= ")";
                }
                else
                {
                    $i++;
                    $que .= " OR (";
                    foreach($aField as $index => $value)
                    {
                        $que .= " $value LIKE '%".$aWord[$i]."%'";
                        if($index != $nField) $que .= " OR ";
                    }
                    $que .= ")";
                }
            }
        }
        else
        {
            if($i>0)
            {
                $que .= " AND ";
            }

            $que .= " (";
            foreach($aField as $index => $value)
            {
                $que .= " $value LIKE '%".$aWord[$i]."%'";
                if($index != $nField) $que .= " OR ";
            }
            $que .= ")";
        }
    }

    return $que;
}

?>


'Web > PHP' 카테고리의 다른 글

mt_implode  (0) 2013.09.26
주 단위 unixtime 반환  (0) 2013.09.26
웹서버/FTP서버 사양알기  (0) 2013.09.26
중복되지 않는 난수  (0) 2013.09.26
디렉토리 생성  (0) 2013.09.26