ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilStr.php
Go to the documentation of this file.
1 <?php
2 /*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2005 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22 */
23 
24 
32 class ilStr
33 {
34  public static function subStr($a_str, $a_start, $a_length = NULL)
35  {
36  if (function_exists("mb_substr"))
37  {
38  return mb_substr($a_str, $a_start, $a_length, "UTF-8");
39  }
40  else
41  {
42  return substr($a_str, $a_start, $a_length);
43  }
44  }
45 
46  function strPos($a_haystack, $a_needle, $a_offset = NULL)
47  {
48  if (function_exists("mb_strpos"))
49  {
50  return mb_strpos($a_haystack, $a_needle, $a_offset, "UTF-8");
51  }
52  else
53  {
54  return strpos($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  public static 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  public static 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  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  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  public static 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  function isUtf8($a_str)
155  {
156  if (function_exists("mb_detect_encoding"))
157  {
158  if (mb_detect_encoding($a_str, "UTF-8") == "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 
194 } // END class.ilUtil
195 ?>