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