ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilStr.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 
13 class ilStr
14 {
15  public static function subStr($a_str, $a_start, $a_length = null)
16  {
17  if (function_exists("mb_substr")) {
18  // bug in PHP < 5.4.12: null is not supported as length (if encoding given)
19  // https://github.com/php/php-src/pull/133
20  if ($a_length === null) {
21  $a_length = mb_strlen($a_str, "UTF-8");
22  }
23 
24  return mb_substr($a_str, $a_start, $a_length, "UTF-8");
25  } else {
26  return substr($a_str, $a_start, $a_length);
27  }
28  }
29 
30  public static function strPos($a_haystack, $a_needle, $a_offset = null)
31  {
32  if (function_exists("mb_strpos")) {
33  return mb_strpos($a_haystack, $a_needle, $a_offset, "UTF-8");
34  } else {
35  return strpos($a_haystack, $a_needle, $a_offset);
36  }
37  }
38 
39  public static function strrPos($a_haystack, $a_needle, $a_offset = null)
40  {
41  if (function_exists("mb_strpos")) {
42  return mb_strrpos($a_haystack, $a_needle, $a_offset, "UTF-8");
43  } else {
44  return strrpos($a_haystack, $a_needle, $a_offset);
45  }
46  }
47 
48  public static function strIPos($a_haystack, $a_needle, $a_offset = null)
49  {
50  if (function_exists("mb_stripos")) {
51  return mb_stripos($a_haystack, $a_needle, $a_offset, "UTF-8");
52  } else {
53  return stripos($a_haystack, $a_needle, $a_offset);
54  }
55  }
56 
57  /*function strrPos($a_haystack, $a_needle, $a_offset = NULL)
58  {
59  if (function_exists("mb_strrpos"))
60  {
61  // only for php version 5.2.0 and above
62  if( version_compare(PHP_VERSION, '5.2.0', '>=') )
63  {
64  return mb_strrpos($a_haystack, $a_needle, $a_offset, "UTF-8");
65  }
66  else
67  {
68  @todo: We need an implementation for php versions < 5.2.0
69  return mb_strrpos($a_haystack, $a_needle, "UTF-8");
70  }
71  }
72  else
73  {
74  return strrpos($a_haystack, $a_needle, $a_offset);
75  }
76  }*/
77 
78  public static function strLen($a_string)
79  {
80  if (function_exists("mb_strlen")) {
81  return mb_strlen($a_string, "UTF-8");
82  } else {
83  return strlen($a_string);
84  }
85  }
86 
87  public static function strToLower($a_string)
88  {
89  if (function_exists("mb_strtolower")) {
90  return mb_strtolower($a_string, "UTF-8");
91  } else {
92  return strtolower($a_string);
93  }
94  }
95 
96  public static function strToUpper($a_string)
97  {
98  $a_string = (string) $a_string;
99  if (function_exists("mb_strtoupper")) {
100  return mb_strtoupper($a_string, "UTF-8");
101  } else {
102  return strtoupper($a_string);
103  }
104  }
105 
109  public static function strCmp($a, $b)
110  {
111  global $DIC;
112 
113  $ilCollator = null;
114  if (isset($DIC["ilCollator"])) {
115  $ilCollator = $DIC["ilCollator"];
116  }
117 
118  if (is_object($ilCollator)) {
119  return ($ilCollator->compare(ilStr::strToUpper($a), ilStr::strToUpper($b)) > 0);
120  } else {
121  return (strcoll(ilStr::strToUpper($a), ilStr::strToUpper($b)) > 0);
122  }
123  }
124 
138  public static function shortenText($a_string, $a_start_pos, $a_num_bytes, $a_encoding = 'UTF-8')
139  {
140  if (function_exists("mb_strcut")) {
141  return mb_strcut($a_string, $a_start_pos, $a_num_bytes, $a_encoding);
142  }
143  return substr($a_string, $a_start_pos, $a_num_bytes);
144  }
145 
149  public static function isUtf8($a_str)
150  {
151  if (function_exists("mb_detect_encoding")) {
152  if (mb_detect_encoding($a_str, "UTF-8", true) == "UTF-8") {
153  return true;
154  }
155  } else {
156  // copied from http://www.php.net/manual/en/function.mb-detect-encoding.php
157  $c=0;
158  $b=0;
159  $bits=0;
160  $len=strlen($a_str);
161  for ($i=0; $i<$len; $i++) {
162  $c=ord($a_str[$i]);
163  if ($c > 128) {
164  if (($c >= 254)) {
165  return false;
166  } elseif ($c >= 252) {
167  $bits=6;
168  } elseif ($c >= 248) {
169  $bits=5;
170  } elseif ($c >= 240) {
171  $bits=4;
172  } elseif ($c >= 224) {
173  $bits=3;
174  } elseif ($c >= 192) {
175  $bits=2;
176  } else {
177  return false;
178  }
179  if (($i+$bits) > $len) {
180  return false;
181  }
182  while ($bits > 1) {
183  $i++;
184  $b=ord($a_str[$i]);
185  if ($b < 128 || $b > 191) {
186  return false;
187  }
188  $bits--;
189  }
190  }
191  }
192  return true;
193  }
194  return false;
195  }
196 
197 
205  public static function strPosAll($a_haystack, $a_needle)
206  {
207  $positions = array();
208  $cpos = 0;
209  while (is_int($pos = strpos($a_haystack, $a_needle, $cpos))) {
210  $positions[] = $pos;
211  $cpos = $pos+1;
212  }
213  return $positions;
214  }
215 
219  public static function replaceFirsOccurence($a_old, $a_new, $a_str)
220  {
221  if (is_int(strpos($a_str, $a_old))) {
222  $a_str = substr_replace($a_str, $a_new, strpos($a_str, $a_old), strlen($a_old));
223  }
224  return $a_str;
225  }
226 
232  public static function convertUpperCamelCaseToUnderscoreCase($value)
233  {
234  return strtolower(preg_replace(
235  array('#(?<=(?:[A-Z]))([A-Z]+)([A-Z][A-z])#', '#(?<=(?:[a-z0-9]))([A-Z])#'),
236  array('\1_\2', '_\1'),
237  $value
238  ));
239  }
240 
249  public static function getBytesForString($a_str)
250  {
251  $bytes = array();
252  for ($i = 0; $i < strlen($a_str); $i++) {
253  $bytes[] = ord($a_str[$i]);
254  }
255  return $bytes;
256  }
257 
264  public static function normalizeUtf8String($a_str)
265  {
266  include_once("./include/Unicode/UtfNormal.php");
267  return UtfNormal::toNFC($a_str);
268  }
269 }
static shortenText($a_string, $a_start_pos, $a_num_bytes, $a_encoding='UTF-8')
Shorten text to the given number of bytes.
Add rich text string
static strLen($a_string)
Definition: class.ilStr.php:78
static convertUpperCamelCaseToUnderscoreCase($value)
Convert a value given in camel case conversion to underscore case conversion (e.g.
static strPos($a_haystack, $a_needle, $a_offset=null)
Definition: class.ilStr.php:30
global $DIC
Definition: saml.php:7
static normalizeUtf8String($a_str)
Normalize UTF8 string.
static strToLower($a_string)
Definition: class.ilStr.php:87
Multi byte sensitive string functions.
Definition: class.ilStr.php:13
static subStr($a_str, $a_start, $a_length=null)
Definition: class.ilStr.php:15
static toNFC($string)
Convert a UTF-8 string to normal form C, canonical composition.
Definition: UtfNormal.php:157
static strIPos($a_haystack, $a_needle, $a_offset=null)
Definition: class.ilStr.php:48
static strrPos($a_haystack, $a_needle, $a_offset=null)
Definition: class.ilStr.php:39
static getBytesForString($a_str)
Return string as byte array Note: Use this for debugging purposes only.
Create styles array
The data for the language used.
static strToUpper($a_string)
Definition: class.ilStr.php:96
$i
Definition: disco.tpl.php:19
static strCmp($a, $b)
Compare two strings.
static strPosAll($a_haystack, $a_needle)
Get all positions of a string.
static replaceFirsOccurence($a_old, $a_new, $a_str)
Replaces the first occurence of $a_old in $a_str with $a_new.
static isUtf8($a_str)
Check whether string is utf-8.