提取字符串中给定搜索字符串周围的X个单词

编程入门 行业动态 更新时间:2024-10-10 19:25:47
本文介绍了提取字符串中给定搜索字符串周围的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个单词

本文发布于:2023-11-30 12:31:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   单词

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!