I have a database in MySQL that i process with PHP. In this database i have a column with long text. People search for some phrases with the search tool on the website. It displays items matching this search.
我在MySQL中有一个用PHP处理的数据库。在这个数据库中,我有一个长文本列。人们使用网站上的搜索工具搜索一些短语。它显示与此搜索匹配的项目。
Now, my question is how to get a part of the text that contains the phrase they search for so that they can see if it's what they looking for?
现在,我的问题是如何获取包含他们搜索的短语的文本的一部分,以便他们可以看到它们是否是他们所寻找的?
For example:
Text: "this is some long text (...) about problems with jQuery and other JavaScript frameworks (...) so check it out"
And now i would like to get for phrase jQuery this:
现在我想得到一个短语jQuery:
about problems with jQuery and other JavaScript frameworks
?
3 个解决方案
#1
1
In MySQL, you can use a combination of LOCATE
(or INSTR
), and SUBSTRING
:
在MySQL中,您可以使用LOCATE(或INSTR)和SUBSTRING的组合:
SELECT SUBSTRING(col, LOCATE('jQuery', col) - 20, 40) AS snippet
FROM yourtable
This will select a 40 character snippet from your text, starting 20 characters before the first occurrence of 'jQuery'
.
这将从您的文本中选择一个40个字符的片段,在第一次出现'jQuery'之前从20个字符开始。
However, it tends to be slow. Alternatives worth looking into:
但是,它往往很慢。备选方案值得研究:
- Using a similar method in PHP
- Using full-text search features from MySQL (not sure if any of them will help), or maybe a whole separate full-text search engine like solr.
在PHP中使用类似的方法
使用MySQL的全文搜索功能(不确定是否有任何帮助),或者像solr一样完整的单独全文搜索引擎。
#3
0
You can use strpos() to find the first occurrence of the phrase you a looking for. Then do a subtraction backwards to get a number less than yo first occurrence. Then call mb_strimwidth(). Here is an example code
您可以使用strpos()查找要查找的短语的第一个匹配项。然后向后减去一个小于第一次出现的数字。然后调用mb_strimwidth()。这是一个示例代码
we will search for the word 'website'.
我们会搜索“网站”这个词。
//Your string
$string = "* is the best *website* there ever is and ever will be. I find so much information here. And its fun and interactive too";
// first occurence of the word '*website*' - how far backwards you want to print
$position=intval(strpos($string, '*website*'))-50;
$display_text=mb_strimwidth($string, $position, 300, '...<a href="link_here">more</a>');
//we will print 300 characters only for display.
echo $display_text;
Like a boss.
像一个老板一样。
#1
1
In MySQL, you can use a combination of LOCATE
(or INSTR
), and SUBSTRING
:
在MySQL中,您可以使用LOCATE(或INSTR)和SUBSTRING的组合:
SELECT SUBSTRING(col, LOCATE('jQuery', col) - 20, 40) AS snippet
FROM yourtable
This will select a 40 character snippet from your text, starting 20 characters before the first occurrence of 'jQuery'
.
这将从您的文本中选择一个40个字符的片段,在第一次出现'jQuery'之前从20个字符开始。
However, it tends to be slow. Alternatives worth looking into:
但是,它往往很慢。备选方案值得研究:
- Using a similar method in PHP
- Using full-text search features from MySQL (not sure if any of them will help), or maybe a whole separate full-text search engine like solr.
在PHP中使用类似的方法
使用MySQL的全文搜索功能(不确定是否有任何帮助),或者像solr一样完整的单独全文搜索引擎。
#2
#3
0
You can use strpos() to find the first occurrence of the phrase you a looking for. Then do a subtraction backwards to get a number less than yo first occurrence. Then call mb_strimwidth(). Here is an example code
您可以使用strpos()查找要查找的短语的第一个匹配项。然后向后减去一个小于第一次出现的数字。然后调用mb_strimwidth()。这是一个示例代码
we will search for the word 'website'.
我们会搜索“网站”这个词。
//Your string
$string = "* is the best *website* there ever is and ever will be. I find so much information here. And its fun and interactive too";
// first occurence of the word '*website*' - how far backwards you want to print
$position=intval(strpos($string, '*website*'))-50;
$display_text=mb_strimwidth($string, $position, 300, '...<a href="link_here">more</a>');
//we will print 300 characters only for display.
echo $display_text;
Like a boss.
像一个老板一样。