1

嗨,我正在进行全文搜索,并在我的函数中查找字符串位置(用于在字符串发生之前和之后剪切 x 字符)我正在使用 php 函数mb_stripos()。每个请求都会调用几次 while(代码如下)。字符串的长度为 500 - 100 000 个字符。

但问题是,在桌面上需要这个时间(每个请求调用几次)cca 500ms,但在服务器上需要 20 000ms。

  • 98% 的请求时间存储在一个长 100 000 个字符的字符串上
  • 通过回声测量microtime()
  • 桌面有 php 7.0.9 和 win7 操作系统和服务器 7.1.3-3+0~20170325135815.21+jessie~1.gbpafff68 和 linux 操作系统
  • apache(桌面或服务器)都有 PHP 加速和 OPcache
  • 它在 symfony fw 上(可能没关系)
  • 大多数 php 操作都在服务器上更快

    while (($lastPos = mb_stripos($content, $searchString, $lastPos)) !== false) {
    
        if($lastPos <= $offset)
            $startStr = 0;
        else
            $startStr = $lastPos - $offset;
    
        $subs[] = mb_substr($content, $startStr, 100);
        $lastPos = $lastPos + strlen($searchString);
    }
    

为什么会有如此可怕的差异?

4

1 回答 1

2

所以问题解决了:缺少库mbstring

使用php 7.1.x时的解决方法:apt-get install php7.1-mbstring

在我们的情况下有一些错误,所以:

apt-get update然后apt-get install php7.1-mbstring重新启动apache。

于 2017-08-28T15:50:45.270 回答