Anybody knows how to search the partial words in database.
For Example -
My String is Twenty-first Century Fox
Database column value is 21st Century Fox
How do I get result using MySQL LIKE or LOCATE or any other query?
What I want to match is - "st Century Fox"
A simple solution would be breaking the string to number of words and search using OR
and LIKE
. In this case.
$keyword = 'Twenty-first Century Fox';
$keywords = explode($keyword,' ');
$keySearch = array();
foreach ($keywords as $keyword){
$keySearch[] = 'column LIKE \'%'.$keyword.'%\'';
}
$query = 'SELECT * FROM tableName WHERE '.implode(' OR ',$keySearch);
You probably should investigate FULLTEXT
searching; it's designed to find stuff like fox
in 21st Century Fox
. It involves creating a special-purpose index and then searching using a MATCH()
predicate.
But understanding that 21st
and Twenty-first
are equivalent is a more complex task. That involves lists of synonyms or other sources of knowledge. If you have to do this you should investigate Sphinx or Lucene.
Use Soundex function campare the two strings...
<?php
$str = "Twenty-first Century Fox";
$str1 = "21st Century Fox";
echo soundex($str);
echo soundex($str1);
?>