ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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 // see https://bugs.php.net/bug.php?id=62703
20 if ($a_length === NULL)
21 {
22 $a_length = self::strLen($a_str);
23 }
24 return mb_substr($a_str, $a_start, $a_length, "UTF-8");
25 }
26 else
27 {
28 return substr($a_str, $a_start, $a_length);
29 }
30 }
31
32 static function strPos($a_haystack, $a_needle, $a_offset = NULL)
33 {
34 if (function_exists("mb_strpos"))
35 {
36 return mb_strpos($a_haystack, $a_needle, $a_offset, "UTF-8");
37 }
38 else
39 {
40 return strpos($a_haystack, $a_needle, $a_offset);
41 }
42 }
43
44 static function strIPos($a_haystack, $a_needle, $a_offset = NULL)
45 {
46 if (function_exists("mb_stripos"))
47 {
48 return mb_stripos($a_haystack, $a_needle, $a_offset, "UTF-8");
49 }
50 else
51 {
52 return stripos($a_haystack, $a_needle, $a_offset);
53 }
54 }
55
56 /*function strrPos($a_haystack, $a_needle, $a_offset = NULL)
57 {
58 if (function_exists("mb_strrpos"))
59 {
60 // only for php version 5.2.0 and above
61 if( version_compare(PHP_VERSION, '5.2.0', '>=') )
62 {
63 return mb_strrpos($a_haystack, $a_needle, $a_offset, "UTF-8");
64 }
65 else
66 {
67 @todo: We need an implementation for php versions < 5.2.0
68 return mb_strrpos($a_haystack, $a_needle, "UTF-8");
69 }
70 }
71 else
72 {
73 return strrpos($a_haystack, $a_needle, $a_offset);
74 }
75 }*/
76
77 static public function strLen($a_string)
78 {
79 if (function_exists("mb_strlen"))
80 {
81 return mb_strlen($a_string, "UTF-8");
82 }
83 else
84 {
85 return strlen($a_string);
86 }
87 }
88
89 static public function strToLower($a_string)
90 {
91 if (function_exists("mb_strtolower"))
92 {
93 return mb_strtolower($a_string, "UTF-8");
94 }
95 else
96 {
97 return strtolower($a_string);
98 }
99 }
100
101 static function strToUpper($a_string)
102 {
103 $a_string = (string) $a_string;
104 if (function_exists("mb_strtoupper"))
105 {
106 return mb_strtoupper($a_string, "UTF-8");
107 }
108 else
109 {
110 return strtoupper($a_string);
111 }
112 }
113
117 static function strCmp($a, $b)
118 {
119 global $ilCollator;
120
121 if (is_object($ilCollator))
122 {
123 return ($ilCollator->compare(ilStr::strToUpper($a), ilStr::strToUpper($b)) > 0);
124 }
125 else
126 {
127 return (strcoll(ilStr::strToUpper($a), ilStr::strToUpper($b)) > 0);
128 }
129 }
130
144 static public function shortenText($a_string,$a_start_pos,$a_num_bytes,$a_encoding = 'UTF-8')
145 {
146 return mb_strcut($a_string, $a_start_pos, $a_num_bytes, $a_encoding);
147 }
148
152 static function isUtf8($a_str)
153 {
154 if (function_exists("mb_detect_encoding"))
155 {
156 if (mb_detect_encoding($a_str, "UTF-8", true) == "UTF-8")
157 {
158 return true;
159 }
160 }
161 else
162 {
163 // copied from http://www.php.net/manual/en/function.mb-detect-encoding.php
164 $c=0; $b=0;
165 $bits=0;
166 $len=strlen($str);
167 for($i=0; $i<$len; $i++){
168 $c=ord($str[$i]);
169 if($c > 128){
170 if(($c >= 254)) return false;
171 elseif($c >= 252) $bits=6;
172 elseif($c >= 248) $bits=5;
173 elseif($c >= 240) $bits=4;
174 elseif($c >= 224) $bits=3;
175 elseif($c >= 192) $bits=2;
176 else return false;
177 if(($i+$bits) > $len) return false;
178 while($bits > 1){
179 $i++;
180 $b=ord($str[$i]);
181 if($b < 128 || $b > 191) return false;
182 $bits--;
183 }
184 }
185 }
186 return true;
187 }
188 return false;
189 }
190
191
199 static public function strPosAll($a_haystack, $a_needle)
200 {
201 $positions = array();
202 $cpos = 0;
203 while(is_int($pos = strpos($a_haystack, $a_needle, $cpos)))
204 {
205 $positions[] = $pos;
206 $cpos = $pos+1;
207 }
208 return $positions;
209 }
210
214 static function replaceFirsOccurence($a_old, $a_new, $a_str)
215 {
216 if (is_int(strpos($a_str, $a_old)))
217 {
218 $a_str = substr_replace ($a_str, $a_new, strpos($a_str, $a_old), strlen($a_old));
219 }
220 return $a_str;
221 }
222}
223?>
Multi byte sensitive string functions.
Definition: class.ilStr.php:14
static strPosAll($a_haystack, $a_needle)
Get all positions of a string.
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:32
static strToLower($a_string)
Definition: class.ilStr.php:89
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:77
static strIPos($a_haystack, $a_needle, $a_offset=NULL)
Definition: class.ilStr.php:44
static subStr($a_str, $a_start, $a_length=NULL)
Definition: class.ilStr.php:15