php - 我如何获取关键字中的文本?

标签 php mysql full-text-search

我在后端使用 PHP 在 mysql 表上进行全文搜索,我希望能够选择用户搜索的关键字前后的一些文本,并像 Google 一样将其显示给他们。

例如,如果用户搜索“他们的世界” 在内容为的列中:

and these children that you spit on, as they try to change their worlds are immune to your consultations. They're quite aware of what they're going through...

结果是这样的:

"...as they try to change their worlds are imm..."

是否可以直接使用 SQL 实现类似的功能,还是我应该获取所有内容并使用 PHP 和正则表达式?

谢谢。

最佳答案

SUBSTRING(column_name,(locate('word',column_name)-20),40) as your_sub_string

就我的想法而言,这样的事情应该可行(无法测试),因为 locate 将返回在 column_name 中首次找到 word 的位置> 和 substring 将返回 column_name 的子字符串,从 column_name 中第一次出现 word 的位置开始减去 20 个字符和 40 个字符的长度。

关于php - 我如何获取关键字中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4238503/

相关文章:

php - 如何从所有表中选择所有数据?

mysql - 如何将 localhost 更改为机器名称?

php - MYSQL 数据库返回错误计数

PHP 搜索查询精确单词匹配 CONCAT

php - 在文件名中使用多个点 (.)

php - 加入 DB::raw() laravel 4

php - 连接数据库失败

Mysql - 来自多个连接表的 'Select max' 没有返回正确的值

caching - RAM 数据存储中的 Elasticsearch

php - 相似搜索和语义搜索