在第一次出现字符串之前和之后返回单词

时间:2021-10-28 21:36:23

I have a body of text returned from a search query, lets call it $body. Now, what I want to do is for the script to find the first occurrence of the search query, $query. I know I can find this first occurrence this with strripos.

我有一个从搜索查询返回的文本正文,我们称之为$ body。现在,我想要做的是让脚本找到第一次出现的搜索查询$ query。我知道我可以通过strripos找到第一次出现。

Once found, I want the script to return a couple of words before the first occurrence of the string as well as a few words after the end of the first occurrence.

一旦找到,我希望脚本在第一次出现字符串之前返回几个单词,并在第一次出现之后返回几个单词。

Essentially I'm trying to do what Google does with it's search results.

基本上我正在努力做谷歌用它的搜索结果。

Any ideas on where I should start? My issue is that I keep returning partial words.

关于我应该从哪里开始的任何想法?我的问题是我不断回复部分词语。

4 个解决方案

#1


You could:

$words = explode(" ", $body);

Creating an array of al the words in $body.

在$ body中创建一个单词数组。

$index  = array_search($query, $words);  
$string = $words[$index - 1]." ".$words[$index]." ".$words[$index + 1];

But you would get into trouble if the query consist out of more than 1 word.

但是如果查询超过1个单词,你会遇到麻烦。

explode - array_search

explode - array_search

#2


Seems to me that you could use strpos to find the beginning of the search term and strlen to get its length, which would let you...

在我看来,你可以使用strpos来找到搜索词的开头,并使用strlen来获取它的长度,这会让你......

  • Create one array from the beginning of the text to the search term and then display the last word in that array and,
  • 从文本的开头到搜索词创建一个数组,然后显示该数组中的最后一个单词,

  • Create a second array from the words after strpos + strlen and display the first word in that array.
  • 从strpos + strlen之后的单词创建第二个数组,并显示该数组中的第一个单词。

Or, if you've already got your hands on some number of characters before and after the search term, if you explode those into an array you can pull out one or two words.

或者,如果您在搜索词之前和之后已经掌握了一些字符数,那么如果将这些字符分解为数组,则可以提取一两个字。

#3


I don't know PHP, but if you can use regular expressions you can use the following one:

我不懂PHP,但如果你可以使用正则表达式,你可以使用以下一个:

string query = "search";
int numberOfWords = 2;
Regex(
       "([^ \t]+\s+){"
     + numberOfWords
     + "}\w*"
     + query
     + "\w*(\s+[^ \t]+){"
     + numberOfWords
     + "}"
);

#4


how about a few words on either side:

两边几句话怎么样:

<?php

$subject= "Four score and seven years ago, our forefathers brought forth upon this continent a new nation";
$pattern= "/(\w+\s){3}forth(\s\w+){3}/";

preg_match($pattern, $subject, $matches);

echo("... $matches[0] ...");

gives:

... our forefathers brought forth upon this continent ...

#1


You could:

$words = explode(" ", $body);

Creating an array of al the words in $body.

在$ body中创建一个单词数组。

$index  = array_search($query, $words);  
$string = $words[$index - 1]." ".$words[$index]." ".$words[$index + 1];

But you would get into trouble if the query consist out of more than 1 word.

但是如果查询超过1个单词,你会遇到麻烦。

explode - array_search

explode - array_search

#2


Seems to me that you could use strpos to find the beginning of the search term and strlen to get its length, which would let you...

在我看来,你可以使用strpos来找到搜索词的开头,并使用strlen来获取它的长度,这会让你......

  • Create one array from the beginning of the text to the search term and then display the last word in that array and,
  • 从文本的开头到搜索词创建一个数组,然后显示该数组中的最后一个单词,

  • Create a second array from the words after strpos + strlen and display the first word in that array.
  • 从strpos + strlen之后的单词创建第二个数组,并显示该数组中的第一个单词。

Or, if you've already got your hands on some number of characters before and after the search term, if you explode those into an array you can pull out one or two words.

或者,如果您在搜索词之前和之后已经掌握了一些字符数,那么如果将这些字符分解为数组,则可以提取一两个字。

#3


I don't know PHP, but if you can use regular expressions you can use the following one:

我不懂PHP,但如果你可以使用正则表达式,你可以使用以下一个:

string query = "search";
int numberOfWords = 2;
Regex(
       "([^ \t]+\s+){"
     + numberOfWords
     + "}\w*"
     + query
     + "\w*(\s+[^ \t]+){"
     + numberOfWords
     + "}"
);

#4


how about a few words on either side:

两边几句话怎么样:

<?php

$subject= "Four score and seven years ago, our forefathers brought forth upon this continent a new nation";
$pattern= "/(\w+\s){3}forth(\s\w+){3}/";

preg_match($pattern, $subject, $matches);

echo("... $matches[0] ...");

gives:

... our forefathers brought forth upon this continent ...