Skip to content

Commit 2d51bc9

Browse files
committed
Fixed issue: cutting of utf8 chars causes json encode error
In attempt to address #1 This fixes the case of the content trimming through UTF8 bytes which would cause errors if left non-utf8 valid.
1 parent 54322cd commit 2d51bc9

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

app/Rss/RssParser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function getPostDataForRssItem(SimpleXMLElement $item): array
5757
{
5858
$date = DateTime::createFromFormat(DateTime::RSS, $item->pubDate ?? '');
5959
$item = [
60-
'title' => substr(strval($item->title ?? ''), 0, 250),
60+
'title' => mb_substr(strval($item->title ?? ''), 0, 250),
6161
'description' => $this->formatDescription(strval($item->description) ?: ''),
6262
'url' => strval($item->link ?? ''),
6363
'guid' => strval($item->guid ?? ''),
@@ -77,7 +77,7 @@ protected function formatDescription(string $description): string
7777
$decoded = preg_replace('/\s+/', ' ', $decoded);
7878

7979
if (strlen($decoded) > 200) {
80-
return substr($decoded, 0, 200) . '...';
80+
return mb_substr($decoded, 0, 200) . '...';
8181
}
8282

8383
return $decoded;
@@ -87,7 +87,7 @@ protected function getPostDataForAtomItem(SimpleXMLElement $item): array
8787
{
8888
$date = new DateTime(strval($item->published ?? $item->updated ?? ''));
8989
return [
90-
'title' => html_entity_decode(substr(strval($item->title ?? ''), 0, 250)),
90+
'title' => html_entity_decode(mb_substr(strval($item->title ?? ''), 0, 250)),
9191
'description' => $this->formatDescription(strval($item->summary) ?: strval($item->content) ?: ''),
9292
'url' => $item->link ? strval($item->link->attributes()['href']) : '',
9393
'guid' => strval($item->id ?? ''),

0 commit comments

Comments
 (0)