Skip to content

Commit 6d622a2

Browse files
authored
Merge pull request #2771 from zachgarwood/fix/slug-encoding
Prevent HasSlug::getUtf8Slug() from guessing encoding by default
2 parents 1b115e6 + 75c4280 commit 6d622a2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/Models/Behaviors/HasSlug.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function handleSlugsOnSave(): void
131131

132132
foreach ($slugParams as $params) {
133133
if (in_array($params['locale'], config('twill.slug_utf8_languages', []))) {
134-
$params['slug'] = $this->getUtf8Slug($params['slug']);
134+
$params['slug'] = $this->getUtf8Slug($params['slug'], ['from_encoding' => mb_list_encodings()]);
135135
} else {
136136
$params['slug'] = Str::slug($params['slug']);
137137
}
@@ -148,7 +148,7 @@ public function handleSlugsOnSave(): void
148148
public function updateOrNewSlug(array $slugParams): void
149149
{
150150
if (in_array($slugParams['locale'], config('twill.slug_utf8_languages', []))) {
151-
$slugParams['slug'] = $this->getUtf8Slug($slugParams['slug']);
151+
$slugParams['slug'] = $this->getUtf8Slug($slugParams['slug'], ['from_encoding' => mb_list_encodings()]);
152152
} else {
153153
$slugParams['slug'] = Str::slug($slugParams['slug']);
154154
}
@@ -451,11 +451,15 @@ public function getUtf8Slug(string $str, array $options = []): string
451451
'lowercase' => true,
452452
'replacements' => [],
453453
'transliterate' => true,
454+
'from_encoding' => null,
454455
];
455456

456457
// Merge options
457458
$options = array_merge($defaults, $options);
458459

460+
// Make sure string is in UTF-8 and strip invalid UTF-8 characters
461+
$str = mb_convert_encoding((string)$str, 'UTF-8', $options['from_encoding']);
462+
459463
$char_map = [
460464
// Latin
461465
'À' => 'A',

0 commit comments

Comments
 (0)