在PHP stristr中搜索并返回周围的字符

时间:2019-02-23 09:09:18

标签: php string text word

在PHP中,我正在使用stristr搜索,但是我还想返回并显示找到的字符串每一侧的周围字符,例如concorancer。大约9或10个字符就足够了,例如: “以下”是我找到的字符串,但是随后我想为我的用户显示上下文,如下所示: “如果我坐在甲板下面”

我当前的代码是

if (stristr($order, $en[$i])) {
$lit= "".$en[$i]."";
$order = str_replace($en[$i], $lit, $order, $count);
$total = $total + $count;
$sub[$loc[$i]] = $sub[$loc[$i]] + $count;
$carreau[$loc[$i]] = $carreau[$loc[$i]]."".$lit." ";
}

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您想要类似$context = substr($order,from,length)

如何根据和长度来计算

使用stripos(和算术)从计算。请注意,该值不少于0。请查看substr doc以了解原因)。

使用strlen(“针”的)和算术确定长度