我在后端使用 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/