您最好使用一组标签,这些标签在插入标题时会被解析并存储在数据库中,然后基于该标签进行查询。
如果你必须解析标题,你基本上会做一个 LIKE 查询:
SELECT * FROM ENTRIES WHERE TITLE LIKE '%<keyword>%';
Run Code Online (Sandbox Code Playgroud)
不过,对于更详细的答案:
// You need some test to see if the word is valid.
// "is" should not be considered a valid match.
// This is a simple one based on length, a
// "blacklist" would be better, but that's up to you.
function isValidEntry( $word )
{
return strlen( $word ) >= 4;
}
//to hold all relevant search strings:
$terms = array();
$postTitleWords = explode( ' ' , strtolower( 'How to Make Coffee' ) );
for( $postTitleWords as $index => $word )
{
if( isValidEntry( $word ) ) $terms[] = $word;
else
{
$bef = @$postTitleWords[ $index - 1 ];
if( $bef && !isValidEntry( $bef ) ) $terms[] = "$bef $word";
$aft = @$postTitleWords[ $index + 1 ];
if( $aft && !isValidEntry( $aft ) ) $terms[] = "$word $aft";
}
}
$terms = array_unique( $terms );
if( !count( $terms ) )
{
//This is a completely unique title!
}
$search = 'SELECT * FROM ENTRIES WHERE lower( TITLE ) LIKE \'%' . implode( '%\' OR lower( TITLE ) LIKE \'%' $terms ) . '\'%';
// either pump that through your mysql_search or PDO.
Run Code Online (Sandbox Code Playgroud)