【问题标题】:In PHP stristr search and return surrounding characters each side在 PHP stristr 中搜索并返回每一侧的周围字符
【发布时间】:2019-02-23 09:09:17
【问题描述】:

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

我当前的代码是

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." ";
}

非常感谢您的帮助。

【问题讨论】:

    标签: php string text word


    【解决方案1】:

    你想要像$context = substr($order,from,length)这样的东西

    长度如何计算?

    使用stripos(和算术)计算来自。请注意它不少于 0。请查看 substr doc 了解原因)。

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

    【讨论】:

    • DinoCoderSaurus 感谢您的建议。我今晚要试试。
    猜你喜欢
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 2013-01-03
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多