ILIAS  Release_4_0_x_branch Revision 61816
 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  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  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  if (function_exists("mb_strtoupper"))
106  {
107  return mb_strtoupper($a_string, "UTF-8");
108  }
109  else
110  {
111  return strtoupper($a_string);
112  }
113  }
114 
118  function strCmp($a, $b)
119  {
120  global $ilCollator;
121 
122  if (is_object($ilCollator))
123  {
124  return ($ilCollator->compare(ilStr::strToUpper($a), ilStr::strToUpper($b)) > 0);
125  }
126  else
127  {
128  return (strcoll(ilStr::strToUpper($a), ilStr::strToUpper($b)) > 0);
129  }
130  }
131 
145  public static function shortenText($a_string,$a_start_pos,$a_num_bytes,$a_encoding = 'UTF-8')
146  {
147  return mb_strcut($a_string, $a_start_pos, $a_num_bytes, $a_encoding);
148  }
149 
153  function isUtf8($a_str)
154  {
155  if (function_exists("mb_detect_encoding"))
156  {
157  if (mb_detect_encoding($a_str, "UTF-8") == "UTF-8")
158  {
159  return true;
160  }
161  }
162  else
163  {
164  // copied from http://www.php.net/manual/en/function.mb-detect-encoding.php
165  $c=0; $b=0;
166  $bits=0;
167  $len=strlen($str);
168  for($i=0; $i<$len; $i++){
169  $c=ord($str[$i]);
170  if($c > 128){
171  if(($c >= 254)) return false;
172  elseif($c >= 252) $bits=6;
173  elseif($c >= 248) $bits=5;
174  elseif($c >= 240) $bits=4;
175  elseif($c >= 224) $bits=3;
176  elseif($c >= 192) $bits=2;
177  else return false;
178  if(($i+$bits) > $len) return false;
179  while($bits > 1){
180  $i++;
181  $b=ord($str[$i]);
182  if($b < 128 || $b > 191) return false;
183  $bits--;
184  }
185  }
186  }
187  return true;
188  }
189  return false;
190  }
191 
192 
193 } // END class.ilUtil
194 ?>