Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix comments search result to work with multibyte strings
Currently, the searching in comments breaks up, if comments contain multibyte characters and string manipulation logic in getRelevantMessagePart happens to cut through them rendering the resulting string invalid (not UTF-8 compliant). This patch replaces all string manipulating functions in this code to multibyte aware ones.

Signed-off-by: Michał Węgrzynek <[email protected]>
  • Loading branch information
mwegrzynek committed Dec 9, 2019
commit 9c3b5fe4bbbb1b602ba6d2290eefcf0a403b3e80
10 changes: 5 additions & 5 deletions apps/comments/lib/Search/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ protected function getVisiblePath(string $path): string {
* @throws NotFoundException
*/
protected function getRelevantMessagePart(string $message, string $search): string {
$start = stripos($message, $search);
$start = mb_stripos($message, $search);
if ($start === false) {
throw new NotFoundException('Comment section not found');
}

$end = $start + strlen($search);
$end = $start + mb_strlen($search);

if ($start <= 25) {
$start = 0;
Expand All @@ -97,15 +97,15 @@ protected function getRelevantMessagePart(string $message, string $search): stri
$prefix = '…';
}

if ((strlen($message) - $end) <= 25) {
$end = strlen($message);
if ((mb_strlen($message) - $end) <= 25) {
$end = mb_strlen($message);
$suffix = '';
} else {
$end += 25;
$suffix = '…';
}

return $prefix . substr($message, $start, $end - $start) . $suffix;
return $prefix . mb_substr($message, $start, $end - $start) . $suffix;
}

}