ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
13class ilStr
14{
15 static public function subStr($a_str, $a_start, $a_length = NULL)
16 {
17 if (function_exists("mb_substr"))
18 {
19 // bug in PHP < 5.4.12: null is not supported as length (if encoding given)
20 // https://github.com/php/php-src/pull/133
21 if ($a_length === null)
22 {
23 $a_length = mb_strlen($a_str, "UTF-8");
24 }
25
26 return mb_substr($a_str, $a_start, $a_length, "UTF-8");
27 }
28 else
29 {
30 return substr($a_str, $a_start, $a_length);
31 }
32 }
33
34 static function strPos($a_haystack, $a_needle, $a_offset = NULL)
35 {
36 if (function_exists("mb_strpos"))
37 {
38 return mb_strpos($a_haystack, $a_needle, $a_offset, "UTF-8");
39 }
40 else
41 {
42 return strpos($a_haystack, $a_needle, $a_offset);
43 }
44 }
45
46 static function strIPos($a_haystack, $a_needle, $a_offset = NULL)
47 {
48 if (function_exists("mb_stripos"))
49 {
50 return mb_stripos($a_haystack, $a_needle, $a_offset, "UTF-8");
51 }
52 else
53 {
54 return stripos($a_haystack, $a_needle, $a_offset);
55 }
56 }
57
58 /*function strrPos($a_haystack, $a_needle, $a_offset = NULL)
59 {
60 if (function_exists("mb_strrpos"))
61 {
62 // only for php version 5.2.0 and above
63 if( version_compare(PHP_VERSION, '5.2.0', '>=') )
64 {
65 return mb_strrpos($a_haystack, $a_needle, $a_offset, "UTF-8");
66 }
67 else
68 {
69 @todo: We need an implementation for php versions < 5.2.0
70 return mb_strrpos($a_haystack, $a_needle, "UTF-8");
71 }
72 }
73 else
74 {
75 return strrpos($a_haystack, $a_needle, $a_offset);
76 }
77 }*/
78
79 static public function strLen($a_string)
80 {
81 if (function_exists("mb_strlen"))
82 {
83 return mb_strlen($a_string, "UTF-8");
84 }
85 else
86 {
87 return strlen($a_string);
88 }
89 }
90
91 static public function strToLower($a_string)
92 {
93 if (function_exists("mb_strtolower"))
94 {
95 return mb_strtolower($a_string, "UTF-8");
96 }
97 else
98 {
99 return strtolower($a_string);
100 }
101 }
102
103 static function strToUpper($a_string)
104 {
105 $a_string = (string) $a_string;
106 if (function_exists("mb_strtoupper"))
107 {
108 return mb_strtoupper($a_string, "UTF-8");
109 }
110 else
111 {
112 return strtoupper($a_string);
113 }
114 }
115
119 static function strCmp($a, $b)
120 {
121 global $ilCollator;
122
123 if (is_object($ilCollator))
124 {
125 return ($ilCollator->compare(ilStr::strToUpper($a), ilStr::strToUpper($b)) > 0);
126 }
127 else
128 {
129 return (strcoll(ilStr::strToUpper($a), ilStr::strToUpper($b)) > 0);
130 }
131 }
132
146 static public function shortenText($a_string,$a_start_pos,$a_num_bytes,$a_encoding = 'UTF-8')
147 {
148 return mb_strcut($a_string, $a_start_pos, $a_num_bytes, $a_encoding);
149 }
150
154 static function isUtf8($a_str)
155 {
156 if (function_exists("mb_detect_encoding"))
157 {
158 if (mb_detect_encoding($a_str, "UTF-8", true) == "UTF-8")
159 {
160 return true;
161 }
162 }
163 else
164 {
165 // copied from http://www.php.net/manual/en/function.mb-detect-encoding.php
166 $c=0; $b=0;
167 $bits=0;
168 $len=strlen($str);
169 for($i=0; $i<$len; $i++){
170 $c=ord($str[$i]);
171 if($c > 128){
172 if(($c >= 254)) return false;
173 elseif($c >= 252) $bits=6;
174 elseif($c >= 248) $bits=5;
175 elseif($c >= 240) $bits=4;
176 elseif($c >= 224) $bits=3;
177 elseif($c >= 192) $bits=2;
178 else return false;
179 if(($i+$bits) > $len) return false;
180 while($bits > 1){
181 $i++;
182 $b=ord($str[$i]);
183 if($b < 128 || $b > 191) return false;
184 $bits--;
185 }
186 }
187 }
188 return true;
189 }
190 return false;
191 }
192
193
201 static public function strPosAll($a_haystack, $a_needle)
202 {
203 $positions = array();
204 $cpos = 0;
205 while(is_int($pos = strpos($a_haystack, $a_needle, $cpos)))
206 {
207 $positions[] = $pos;
208 $cpos = $pos+1;
209 }
210 return $positions;
211 }
212
216 static function replaceFirsOccurence($a_old, $a_new, $a_str)
217 {
218 if (is_int(strpos($a_str, $a_old)))
219 {
220 $a_str = substr_replace ($a_str, $a_new, strpos($a_str, $a_old), strlen($a_old));
221 }
222 return $a_str;
223 }
224
230 public static function convertUpperCamelCaseToUnderscoreCase($value) {
231 return preg_replace('/(^|[a-z])([A-Z])/e', 'strtolower(strlen("\\1") ? "\\1_\\2" : "\\2")', $value);
232 }
233
242 static function getBytesForString($a_str)
243 {
244 $bytes = array();
245 for($i = 0; $i < strlen($a_str); $i++)
246 {
247 $bytes[] = ord($a_str[$i]);
248 }
249 return $bytes;
250 }
251
258 function normalizeUtf8String($a_str)
259 {
260 include_once("./include/Unicode/UtfNormal.php");
261 return UtfNormal::toNFC($a_str);
262 }
263
264
265}
266?>
toNFC( $string)
Convert a UTF-8 string to normal form C, canonical composition.
Definition: UtfNormal.php:157
Multi byte sensitive string functions.
Definition: class.ilStr.php:14
static convertUpperCamelCaseToUnderscoreCase($value)
Convert a value given in camel case conversion to underscore case conversion (e.g.
static strPosAll($a_haystack, $a_needle)
Get all positions of a string.
static getBytesForString($a_str)
Return string as byte array Note: Use this for debugging purposes only.
static shortenText($a_string, $a_start_pos, $a_num_bytes, $a_encoding='UTF-8')
Shorten text to the given number of bytes.
static strPos($a_haystack, $a_needle, $a_offset=NULL)
Definition: class.ilStr.php:34
static strToLower($a_string)
Definition: class.ilStr.php:91
normalizeUtf8String($a_str)
Normalize UTF8 string.
static isUtf8($a_str)
Check whether string is utf-8.
static replaceFirsOccurence($a_old, $a_new, $a_str)
Replaces the first occurence of $a_old in $a_str with $a_new.
static strCmp($a, $b)
Compare two strings.
static strToUpper($a_string)
static strLen($a_string)
Definition: class.ilStr.php:79
static strIPos($a_haystack, $a_needle, $a_offset=NULL)
Definition: class.ilStr.php:46
static subStr($a_str, $a_start, $a_length=NULL)
Definition: class.ilStr.php:15