获取指定单词后面的两个单词


Get next two words after a specified word

我需要从预定义的文本块中获取指定字符串之后的下一个单词,例如:如果我的话是"作者"并且需要返回"约翰史密斯" 任何帮助将不胜感激,这纯粹是PHP

Tibor说的...粘贴您尝试破译的确切字符串...

即,如果您的字符串是这样的:

Title: The Book That Never Booked
Author: Richard McPoopsalot
Published Date: 06/06/1966

然后你可以做这样的事情:

preg_match('/(?<=Author: )'S+/i', $string, $match);
$authorName = $match[0];
echo $authorName . "is the author of that book"

试试这个正则表达式:

/author:'s*('w+'s'w+)/i

它匹配单词"author:",然后是一个空格,然后是接下来的两个单词。 但是,它只会捕获最后两个单词。

这是我

的版本,基于对您的输入的假设。我会使用正则表达式,因为这是他们真正擅长的事情之一:

$string = "This is a test context for author John Smith.";
preg_match('/author''s(''w+''s''w+)/', $string, $matches);
echo $matches[1];

输出:

John Smith