如何得到用stripos()得到的字符的完整单词? (PHP)

How to get the complete word of the character that is obtained with stripos()? (PHP)

我找不到指示如何操作的文档。我在我网站的搜索结果中动态显示 post 描述的一部分。

示例:

<?php
$extract = "Include all the information someone would need to answer your question.";
$search = "format";
$num = stripos($extract,$search);

$to_show = substr($extract,$num, 17);

echo $to_show;
?>

结果:

formation someone

我希望能够显示“信息”而不是“编队”。有什么建议吗?

实际上,正则表达式可以很好地解决您的特定问题。使用 preg_match_all:

搜索 \w*format\w*
$extract = "Include all the information someone would need to answer your question.";
preg_match_all("/\w*format\w*/", $extract, $matches);
print_r($matches[0]);

这会打印:

Array
(
    [0] => information
)