Search code examples
phpstringsymfonystripos

mb_stripos huge time difference


Hi I am working on full text search and in my function where i finding string position (for cut x chars before and after string occur) i am using php function mb_stripos(). There is while (code bellow) called few times per request. Strings are from 500 - 100 000 chars long.

But problem is, that on desktop takes this while (called few times per request) cca 500ms, but on server it takes 20 000ms.

  • 98% of request time it stock on one string long 100 000 characters
  • measured by echoing microtime()
  • desktop has php 7.0.9 and win7 os and server 7.1.3-3+0~20170325135815.21+jessie~1.gbpafff68 with linux os
  • both apaches (desktop or server) has PHP acceleration and OPcache
  • it is on symfony fw (it will not matter probably)
  • most php operations are on server quicker

    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);
    }
    

Why so horrible difference?


Solution

  • So problem solved: missing library mbstring.

    Solution when using php 7.1.x: apt-get install php7.1-mbstring

    In our situation there was some errors so:

    apt-get update and after that apt-get install php7.1-mbstring and restart apache.