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 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包