ILIAS  release_8 Revision v8.24
class.ilStr.php
Go to the documentation of this file.
1<?php
2
22class ilStr
23{
24 public static function subStr(string $a_str, int $a_start, ?int $a_length = null): string
25 {
26 if (function_exists("mb_substr")) {
27 // bug in PHP < 5.4.12: null is not supported as length (if encoding given)
28 // https://github.com/php/php-src/pull/133
29 if ($a_length === null) {
30 $a_length = mb_strlen($a_str, "UTF-8");
31 }
32
33 return mb_substr($a_str, $a_start, $a_length, "UTF-8");
34 } else {
35 return substr($a_str, $a_start, $a_length);
36 }
37 }
38
42 public static function strPos(string $a_haystack, string $a_needle, ?int $a_offset = null)
43 {
44 if (function_exists("mb_strpos")) {
45 return mb_strpos($a_haystack, $a_needle, $a_offset, "UTF-8");
46 } else {
47 return strpos($a_haystack, $a_needle, $a_offset);
48 }
49 }
50
54 public static function strIPos(string $a_haystack, string $a_needle, ?int $a_offset = null)
55 {
56 if (function_exists("mb_stripos")) {
57 return mb_stripos($a_haystack, $a_needle, $a_offset ?? 0, "UTF-8");
58 } else {
59 return stripos($a_haystack, $a_needle, $a_offset);
60 }
61 }
62
63 public static function strLen(string $a_string): int
64 {
65 if (function_exists("mb_strlen")) {
66 return mb_strlen($a_string, "UTF-8");
67 } else {
68 return strlen($a_string);
69 }
70 }
71
72 public static function strToLower(string $a_string): string
73 {
74 if (function_exists("mb_strtolower")) {
75 return mb_strtolower($a_string, "UTF-8");
76 } else {
77 return strtolower($a_string);
78 }
79 }
80
81 public static function strToUpper(string $a_string): string
82 {
83 if (function_exists("mb_strtoupper")) {
84 return mb_strtoupper($a_string, "UTF-8");
85 } else {
86 return strtoupper($a_string);
87 }
88 }
89
90 public static function strCmp(string $a, string $b): int
91 {
92 return strcoll(ilStr::strToUpper($a), ilStr::strToUpper($b));
93 }
94
102 public static function shortenText(
103 string $a_string,
104 int $a_start_pos,
105 int $a_num_bytes,
106 string $a_encoding = 'UTF-8'
107 ): string {
108 if (function_exists("mb_strcut")) {
109 return mb_strcut($a_string, $a_start_pos, $a_num_bytes, $a_encoding);
110 }
111 return substr($a_string, $a_start_pos, $a_num_bytes);
112 }
113
117 public static function isUtf8(string $a_str): bool
118 {
119 if (function_exists("mb_detect_encoding")) {
120 if (mb_detect_encoding($a_str, "UTF-8", true) === "UTF-8") {
121 return true;
122 }
123 } else {
124 // copied from http://www.php.net/manual/en/function.mb-detect-encoding.php
125 $c = 0;
126 $b = 0;
127 $bits = 0;
128 $len = strlen($a_str);
129 for ($i = 0; $i < $len; $i++) {
130 $c = ord($a_str[$i]);
131 if ($c > 128) {
132 if (($c >= 254)) {
133 return false;
134 } elseif ($c >= 252) {
135 $bits = 6;
136 } elseif ($c >= 248) {
137 $bits = 5;
138 } elseif ($c >= 240) {
139 $bits = 4;
140 } elseif ($c >= 224) {
141 $bits = 3;
142 } elseif ($c >= 192) {
143 $bits = 2;
144 } else {
145 return false;
146 }
147 if (($i + $bits) > $len) {
148 return false;
149 }
150 while ($bits > 1) {
151 $i++;
152 $b = ord($a_str[$i]);
153 if ($b < 128 || $b > 191) {
154 return false;
155 }
156 $bits--;
157 }
158 }
159 }
160 return true;
161 }
162 return false;
163 }
164
171 public static function convertUpperCamelCaseToUnderscoreCase(string $value): string
172 {
173 return strtolower(
174 preg_replace(
175 ['#(?<=(?:[A-Z]))([A-Z]+)([A-Z][A-z])#', '#(?<=(?:[a-z0-9]))([A-Z])#'],
176 ['\1_\2', '_\1'],
177 $value
178 )
179 );
180 }
181
185 public static function shortenTextExtended(
186 string $a_str,
187 int $a_len,
188 bool $a_dots = false,
189 bool $a_next_blank = false,
190 bool $a_keep_extension = false
191 ): string {
192 if (ilStr::strLen($a_str) > $a_len) {
193 /*
194 * When adding dots, the dots have to be included in the length such
195 * that the total length of the resulting string does not exceed
196 * the given maximum length (see BT 33865).
197 */
198 if ($a_dots) {
199 $a_len--;
200 }
201 if ($a_next_blank) {
202 $len = ilStr::strPos($a_str, " ", $a_len);
203 } else {
204 $len = $a_len;
205 }
206 // BEGIN WebDAV
207 // - Shorten names in the middle, before the filename extension
208 // Workaround for Windows WebDAV Client:
209 // Use the unicode ellipsis symbol for shortening instead of
210 // three full stop characters.
211 $p = false;
212 if ($a_keep_extension) {
213 $p = strrpos($a_str, '.'); // this messes up normal shortening, see bug #6190
214 }
215 if ($p === false || $p == 0 || strlen($a_str) - $p > $a_len) {
216 $a_str = ilStr::subStr($a_str, 0, $len);
217 if ($a_dots) {
218 $a_str .= "\xe2\x80\xa6"; // UTF-8 encoding for Unicode ellipsis character.
219 }
220 } else {
221 if ($a_dots) {
222 $a_str = ilStr::subStr($a_str, 0, $len - (strlen($a_str) - $p + 1)) . "\xe2\x80\xa6" . substr(
223 $a_str,
224 $p
225 );
226 } else {
227 $a_str = ilStr::subStr($a_str, 0, $len - (strlen($a_str) - $p + 1)) . substr($a_str, $p);
228 }
229 }
230 }
231
232 return $a_str;
233 }
234
241 public static function shortenWords(string $a_str, int $a_len = 30, bool $a_dots = true): string
242 {
243 $str_arr = explode(" ", $a_str);
244
245 for ($i = 0; $i < count($str_arr); $i++) {
246 if (ilStr::strLen($str_arr[$i]) > $a_len) {
247 $str_arr[$i] = ilStr::subStr($str_arr[$i], 0, $a_len);
248 if ($a_dots) {
249 $str_arr[$i] .= "...";
250 }
251 }
252 }
253
254 return implode(" ", $str_arr);
255 }
256}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.ilStr.php:23
static strPos(string $a_haystack, string $a_needle, ?int $a_offset=null)
Definition: class.ilStr.php:42
static strIPos(string $a_haystack, string $a_needle, ?int $a_offset=null)
Definition: class.ilStr.php:54
static strCmp(string $a, string $b)
Definition: class.ilStr.php:90
static shortenWords(string $a_str, int $a_len=30, bool $a_dots=true)
Ensure that the maximum word lenght within a text is not longer than $a_len.
static strToLower(string $a_string)
Definition: class.ilStr.php:72
static shortenTextExtended(string $a_str, int $a_len, bool $a_dots=false, bool $a_next_blank=false, bool $a_keep_extension=false)
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:24
static convertUpperCamelCaseToUnderscoreCase(string $value)
Convert a value given in camel case conversion to underscore case conversion (e.g.
static shortenText(string $a_string, int $a_start_pos, int $a_num_bytes, string $a_encoding='UTF-8')
Shorten text to the given number of bytes.
static strLen(string $a_string)
Definition: class.ilStr.php:63
static strToUpper(string $a_string)
Definition: class.ilStr.php:81
static isUtf8(string $a_str)
Check whether string is utf-8.
$c
Definition: cli.php:38
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
$i
Definition: metadata.php:41
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples