Skip to content

Commit 207a4da

Browse files
committed
Improve encoding detection
Signed-off-by: Raul <[email protected]>
1 parent 4a725a3 commit 207a4da

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"jakub-onderka/php-parallel-lint": "^1.0.0",
88
"nextcloud/coding-standard": "^1.0.0",
99
"psalm/phar": "^4.3",
10-
"phpunit/phpunit": "^9.5"
10+
"phpunit/phpunit": "^9.5",
11+
"ext-mbstring": "*"
1112
},
1213
"license": "AGPLv3",
1314
"authors": [

lib/Service/EncodingService.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,28 @@
2626
namespace OCA\Text\Service;
2727

2828
class EncodingService {
29-
public const COMMON_ENCODINGS = ['ASCII', 'UTF-8', 'ISO-8859-1', 'ISO-8859-15', 'Windows-1252'];
29+
public const COMMON_ENCODINGS = ['UTF-8', 'Windows-1252', 'ISO-8859-1', 'ISO-8859-15', 'ASCII'];
3030

3131
public function encodeToUtf8(string $string): ?string {
32-
$encoding = mb_detect_encoding($string, $this->getEncodings(), true);
32+
$encoding = $this->detectEncoding($string);
3333
if (!$encoding) {
3434
return null;
3535
}
3636

3737
return mb_convert_encoding($string, 'UTF-8', $encoding);
3838
}
3939

40+
public function detectEncoding(string $string): ?string {
41+
$encodings = $this->getEncodings();
42+
foreach ($encodings as $encoding) {
43+
if (mb_check_encoding($string, $encoding)) {
44+
return $encoding;
45+
}
46+
}
47+
48+
return null;
49+
}
50+
4051
/**
4152
* @return string[]
4253
*/

0 commit comments

Comments
 (0)