blob: 672e60df148ad500c7ea5dfcd1cc364101248889 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace LdapRecord\Models\Attributes;
4
5class MbString
6{
7 /**
8 * Get the integer value of a specific character.
9 *
10 * @param $string
11 *
12 * @return int
13 */
14 public static function ord($string)
15 {
16 if (static::isLoaded()) {
17 $result = unpack('N', mb_convert_encoding($string, 'UCS-4BE', 'UTF-8'));
18
19 if (is_array($result)) {
20 return $result[1];
21 }
22 }
23
24 return ord($string);
25 }
26
27 /**
28 * Get the character for a specific integer value.
29 *
30 * @param $int
31 *
32 * @return string
33 */
34 public static function chr($int)
35 {
36 if (static::isLoaded()) {
37 return mb_convert_encoding(pack('n', $int), 'UTF-8', 'UTF-16BE');
38 }
39
40 return chr($int);
41 }
42
43 /**
44 * Split a string into its individual characters and return it as an array.
45 *
46 * @param string $value
47 *
48 * @return string[]
49 */
50 public static function split($value)
51 {
52 return preg_split('/(?<!^)(?!$)/u', $value);
53 }
54
55 /**
56 * Detects if the given string is UTF 8.
57 *
58 * @param $string
59 *
60 * @return string|false
61 */
62 public static function isUtf8($string)
63 {
64 if (static::isLoaded()) {
65 return mb_detect_encoding($string, 'UTF-8', $strict = true);
66 }
67
68 return $string;
69 }
70
71 /**
72 * Checks if the mbstring extension is enabled in PHP.
73 *
74 * @return bool
75 */
76 public static function isLoaded()
77 {
78 return extension_loaded('mbstring');
79 }
80}