MySQL 可以很好地处理全文搜索,
但它不会突出显示搜索的关键字,
我怎样才能最有效地做到这一点?
The solutions posed above require retrieval of the entire document in order to search, replace, and highlight text. If the document is large, and many are, this seems like a really bad idea. Better would be for MySQL FTS to return the text offsets directly like SQLITE does, then use an indexed substring operator - that would be significantly more efficient.
执行您的 sql 查询,然后对结果执行 preg_replace,将每个关键字替换为 KeyWord
$hilitedText = preg_replace ( '/keyword/' , '/<span class="hilite">keyword<\/span>/' , $row['columName']);
并在您的 css 中定义 hilite 类以格式化您希望突出显示的关键字。如果您有多个关键字,请将它们放在一个数组中,并将它们的替换以相同的顺序放在第二个数组中,并将这些数组作为函数的前两个参数传递。
从 mysql 获取结果集。对每个搜索词进行搜索和替换,将每个词替换为您为突出显示所做的任何事情,例如,<span class='highlight'>word</span>