Skip to content

Commit

Permalink
Merge pull request #16 from helsingborg-stad/fix/malformed-utf8-esape
Browse files Browse the repository at this point in the history
Fix/malformed utf8 esape
  • Loading branch information
sebastianthulin authored May 21, 2024
2 parents 308c120 + d19d927 commit 80d1f56
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
14 changes: 13 additions & 1 deletion source/php/Index.Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,19 @@ public function testThatABlockPostIsGeneratingAExcerpt() {
$this->assertNotEmpty($truncatedExcerpt);
}

}
public function testThatMalformedUTF8ContentIsFixed() {

// Given
$post = [
"post_title" => "Test Post",
"post_content" => "R\xc3\xb8d P\xc3\xb8lse 🌭"
];

// When
$post = Index::utf8ize($post);

// Then
$this->assertEquals("Rød Pølse 🌭", $post['post_content']);
}

}
23 changes: 23 additions & 0 deletions source/php/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public function index($post)
}
});

$post = self::utf8ize($post); // UTF-8 Escape

//Index post
if (self::recordToLarge($post)) {
$splitRecord = self::splitRecord($post);
Expand Down Expand Up @@ -462,4 +464,25 @@ private static function getPostAndPostId($post)

return [$post, $postId];
}

/**
* Convert data to utf-8
*
* @param mixed $data
* @return mixed
*/
public static function utf8ize($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
$data[$key] = self::utf8ize($value);
}
} else if (is_object($data)) {
foreach ($data as $key => $value) {
$data->$key = self::utf8ize($value);
}
} else if (is_string($data)) {
return mb_convert_encoding($data, 'UTF-8');
}
return $data;
}
}

0 comments on commit 80d1f56

Please sign in to comment.