提取字符串中给定搜索字符串周围的X个单词 [英] Extract X number of words surrounding a given search string within a string

查看:115
本文介绍了提取字符串中给定搜索字符串周围的X个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来在搜索中提取给定单词两侧的X个单词.

I am looking for a way to extract X number of words on either side of a given word in a search.

例如,如果用户输入"inmate"作为搜索词,而MySQL查询找到了在帖子内容中包含"inmate"的帖子,那么我不希望返回该帖子的全部内容,而只是返回x两侧的单词数可以使用户了解该帖子的要旨,然后他们可以决定是否要继续阅读该帖子并完整阅读该帖子.

For example, if a user enters "inmate" as a search word and the MySQL query finds a post that contains "inmate" in the content of the post, I would like to return not the entire contents of the post but just x number of words on either side of it to give the user the gist of the post and then they can decide if they want to continue on to the post and read it in full.

我正在使用PHP.

谢谢!

推荐答案

您可能无法使用正则表达式完全解决此问题.单词之间其他字符的可能性太多...

You might not be able to fully solve this problem with regex. There are too many possibilities of other characters between the words...

但是您可以尝试使用此正则表达式:

But you can try this regex:

((?:\S+\s*){0,5}\S*inmate\S*(?:\s*\S+){0,5})

请参阅此处:红斑

您可能还希望排除某些字符,因为它们不算作单词.现在,正则表达式会将由空格包围的任何非空格字符序列都视为单词.

You might also want to exclude certain characters as they are not counted as words. Right now the regex counts any sequence of non space characters that are surrounded by spaces as word.

仅匹配实词:

((?:\w+\s*){0,5}<search word>(?:\s*\w+){0,5})

但是这里任何非单词字符(,"等)都会阻止匹配.

But here any non word character (,". etc.) brakes the matching.

所以你可以继续...

So you can go on...

((?:[\w"',.-]+\s*){0,5}["',.-]?<search word>["',.-]?(?:\s*[\w"',.-]+){0,5})

这还将在您的搜索词周围将5个单词与',.-"之一匹配.

This would also match 5 words with one of "',.- around your search term.

要在php中使用它:

$sourcestring="For example, if a user enters \"inmate\" as a search word and the MySQL";
preg_match_all('/(?:\S+\s*){0,5}\S*inmate\S*(?:\s*\S+){0,5}/s',$sourcestring,$matches);
echo $matches[0][0]; // you might have more matches, they will be in $matches[0][x]

这篇关于提取字符串中给定搜索字符串周围的X个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆