|
|
@ -43,32 +43,42 @@ class SearchAndDisplace |
|
|
|
$replacements[$searcher['key']] = $searcher['replace_with']; |
|
|
|
} |
|
|
|
|
|
|
|
$updatedDocumentContent = ''; |
|
|
|
$currentIndex = 0; |
|
|
|
$searchers = []; |
|
|
|
|
|
|
|
foreach ($searchResult as $searcher => $searcherResults) { |
|
|
|
$replacementIndexes[$searcher] = []; |
|
|
|
|
|
|
|
foreach ($searcherResults as $searcherResult) { |
|
|
|
$partialContent = substr($this->documentContent, $currentIndex, $searcherResult['start'] - $currentIndex); |
|
|
|
$searchers[$searcherResult['start']] = array_merge($searcherResult, [ |
|
|
|
'searcher' => $searcher |
|
|
|
]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$updatedDocumentContent = $updatedDocumentContent . $partialContent; |
|
|
|
ksort($searchers); |
|
|
|
|
|
|
|
$start = strlen($updatedDocumentContent); |
|
|
|
$updatedDocumentContent = ''; |
|
|
|
$currentIndex = 0; |
|
|
|
|
|
|
|
foreach ($searchers as $start => $searcherItem) { |
|
|
|
$partialContent = substr($this->documentContent, $currentIndex, $searcherItem['start'] - $currentIndex); |
|
|
|
|
|
|
|
$updatedDocumentContent = $updatedDocumentContent . $replacements[$searcher]; |
|
|
|
$updatedDocumentContent = $updatedDocumentContent . $partialContent; |
|
|
|
|
|
|
|
$end = strlen($updatedDocumentContent) - 1; |
|
|
|
$start = strlen($updatedDocumentContent); |
|
|
|
|
|
|
|
if ($start <= $end) { |
|
|
|
$replacementIndexes[$searcher][] = [ |
|
|
|
'start' => $start, |
|
|
|
'end' => $end, |
|
|
|
]; |
|
|
|
} |
|
|
|
$updatedDocumentContent = $updatedDocumentContent . $replacements[$searcherItem['searcher']]; |
|
|
|
|
|
|
|
$currentIndex = $searcherResult['end'] + 1; |
|
|
|
$end = strlen($updatedDocumentContent) - 1; |
|
|
|
|
|
|
|
if ($start <= $end) { |
|
|
|
$replacementIndexes[$searcherItem['searcher']][] = [ |
|
|
|
'start' => $start, |
|
|
|
'end' => $end, |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
$currentIndex = $searcherItem['end'] + 1; |
|
|
|
} |
|
|
|
|
|
|
|
if ($currentIndex < strlen($this->documentContent) - 1) { |
|
|
|