Skip to content

Commit d5fee19

Browse files
committed
refactor: remove deprecated utf8_encode
1 parent 5f69603 commit d5fee19

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

server/services/search.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,35 @@ function packParamsJSON($params_array, $post_params)
2828
return json_encode($output_array);
2929
}
3030

31+
function encode_string(string $str, string $to_encoding = 'UTF-8'): string {
32+
// Defining constants with known encodings and default one
33+
$DEFAULT_ENCODING = 'ISO-8859-1';
34+
$ENCODINGS = [
35+
'UTF-8',
36+
'Windows-1251',
37+
'ISO-8859-1',
38+
'ASCII',
39+
'KOI8-R',
40+
'CP866'
41+
];
42+
43+
// Trying to define the received string encoding
44+
$str_encoding = mb_detect_encoding($str, $ENCODINGS, true);
45+
46+
// If the received string encoding is unknown the default one will be used
47+
if ($str_encoding === false) {
48+
$str_encoding = $DEFAULT_ENCODING;
49+
}
50+
51+
// Converting encoding and returning updated string back
52+
return mb_convert_encoding($str, $to_encoding, $str_encoding);
53+
}
54+
3155
function utf8_converter($array)
3256
{
3357
array_walk_recursive($array, function (&$item, $key) {
3458
if ($item !== null && !mb_detect_encoding($item, 'utf-8', true)) {
35-
$item = utf8_encode($item);
59+
$item = encode_string($item);
3660
}
3761
});
3862

0 commit comments

Comments
 (0)