<?php 


function getCho($col, $idx, $f_word)
{
	// echo "getCho:" . $idx . "," . $f_word . "<br>";
    if($f_word == 'ㄱ')
		return " substr({$col},{$idx},1) between '가' and '낗'";
	else if($f_word == 'ㄴ')
		return " substr({$col},{$idx},1) between '나' and '닣'";
	else if($f_word == 'ㄷ')
		return " substr({$col},{$idx},1) between '다' and '띻'";
	else if($f_word == 'ㄹ')
		return " substr({$col},{$idx},1) between '라' and '맇'";
	else if($f_word == 'ㅁ')
		return " substr({$col},{$idx},1) between '마' and '밓'";
	else if($f_word == 'ㅂ')
		return " substr({$col},{$idx},1) between '바' and '삫'";
	else if($f_word == 'ㅅ')
		return " substr({$col},{$idx},1) between '사' and '앃'";
	else if($f_word == 'ㅇ')
		return " substr({$col},{$idx},1) between '아' and '잏'";
	else if($f_word == 'ㅈ')
		return " substr({$col},{$idx},1) between '자' and '찧'";
	else if($f_word == 'ㅊ')
		return " substr({$col},{$idx},1) between '차' and '칳'";
	else if($f_word == 'ㅋ')
		return " substr({$col},{$idx},1) between '카' and '킿'";
	else if($f_word == 'ㅌ')
		return " substr({$col},{$idx},1) between '타' and '팋'";
	else if($f_word == 'ㅍ')
		return " substr({$col},{$idx},1) between '파' and '핗'";
	else if($f_word == 'ㅎ')
		return " substr({$col},{$idx},1) between '하' and '힣'";
	else
		return " substr({$col},{$idx},1) = '{$f_word}'";
}


function splitCho($term) {
    $i = 0;
    $chos = array();
    for ($i = 0; $i < mb_strlen($term,"UTF-8"); $i++) {
        $char = mb_substr($term, $i, 1, 'UTF-8');
        array_push($chos, $char);
    }
    return $chos;
}

function getSqlCho($colume, $term) {    
    $sql = "";
    $i = 1;
    foreach (splitCho($term) as $ch) {
        $sql .= ($i == 1 ? "" : " AND ") . getCho($colume, $i++, $ch);
    }    
    return $sql;
}