contao/test — Issue #2520: Endless loop in search index
Author: aschempp
Created: 2011-11-29 09:49:40+00:00
State: open
Labels: Defect, framework, Invalid

Today I came to a very strange problem with the search index. system/libraries/Search.php on about Line 105 has two while-loops to remove unwanted javascript and non-indexing stuff. However, in some special cases, the while loops never ends. In my particular case, it was the first loop never stopping. After searching for more than two hours, I found the reason and a solution to the problem.

```
// Strip JavaScript (thanks to Pieter Dreef)
while (preg_match('/<\/script>/i', $strContent, $arrInner, PREG_OFFSET_CAPTURE, (strlen($arrOuter[0][0]) + $arrOuter[0][1])))
{
break;
}

$strContent = substr($strContent, 0, $arrOuter[0][1]) . substr($strContent, (strlen($arrInner[0][0]) + $arrInner[0][1]));
}

// Strip non-indexable areas (thanks to Pieter Dreef)
while (preg_match('/<)-- indexer::stop -->/', $strContent, $arrOuter, PREG_OFFSET_CAPTURE))
{
if (

![](preg_match('/<)-- indexer::continue -->/', $strContent, $arrInner, PREG_OFFSET_CAPTURE, (strlen($arrOuter[0][0]) + $arrOuter[0][1])))
{
break;
}

$strContent = substr($strContent, 0, $arrOuter[0][1]) . substr($strContent, (strlen($arrInner[0][0]) + $arrInner[0][1]));
}
```

The problem is, PREG_OFFSET_CAPTURE is not binary safe, and therefore returning wrong positions. In my case, there were so much unicode characters, the <\/?script[^>]*>)|()/i', $strContent);
$strContent = '';

for($_rit=0, $total=count($matches); $_rit<$total; $_rit=$_rit+2)
{
$strContent .= $matches[$_rit];
}
```

Basically, split the content with