내가 제목에 따라 희망 시작하기에 충분해야한다, (모두 strpos
및 similar_text
사용) 해낸 것입니다. 이렇게하면 구뿐만 아니라 한 단어 검색도 허용되며 구두점은 무시됩니다.
function search($haystack, $needle) {
// remove punctuation
$haystack = preg_replace('/[^a-zA-Z 0-9]+/', '', $haystack);
// look for exact match
if (stripos($haystack, $needle)) {
return true;
}
// look for similar match
$words = explode(' ', $haystack);
$total_words = count($words);
$total_search_words = count(explode(' ', $needle));
for ($i = 0; $i < $total_words; $i++) {
// make sure the number of words we're searching for
// don't exceed the number of words remaining
if (($total_words - $i) < $total_search_words) {
break;
}
// compare x-number of words at a time
$temp = implode(' ', array_slice($words, $i, $total_search_words));
$percent = 0;
similar_text($needle, $temp, $percent);
if ($percent >= 80) {
return true;
}
}
return false;
}
$text = "What year did George Washingtin become president?";
$keyword = "Washington";
if (search($text, $keyword)) {
echo 'looks like a match!';
}
먼저 맞춤법 검사기를 통해 실행합니다. – Jaime