dongxinpa3101 2019-02-23 09:09
浏览 76
已采纳

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

In PHP I am using stristr search but I also want to return and display the surrounding characters each side of the found string, like concordancer. About 9 or 10 characters should be enough, for example: 'below' is the string I have found, but then I want to display context for my users, like this: 'if I sat below the deck'

My current code is

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

Thank you so much for any help.

  • 写回答

1条回答 默认 最新

  • duanlu9557 2019-02-23 14:02
    关注

    You want something like $context = substr($order,from,length)

    How to calculate from and length?

    Use stripos (and arithmetic) to calculate from. Beware it isn't less than 0. Review substr doc for the why).

    Use strlen (of the "needle") and arithmetic to determine the length.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置