added japanese language
[openemr.git] / phpmyadmin / libraries / String.class.php
bloba5fbe8a0fff7e7b5b4cfd829fb650bd5ccfc9a56
1 <?php
2 /**
3 * Specialized string class for phpMyAdmin.
4 * The SQL Parser code relies heavily on these functions.
6 * @package PhpMyAdmin-String
7 */
9 /**
10 * Specialized string class for phpMyAdmin.
11 * The SQL Parser code relies heavily on these functions.
13 * @package PhpMyAdmin-String
15 class PMA_String
17 /**
18 * @var PMA_StringType
20 private $_type;
21 /**
22 * @var PMA_StringByte
24 private $_byte;
26 /**
27 * Constructor
29 public function __construct()
31 if (@function_exists('mb_strlen')) {
32 mb_internal_encoding('utf-8');
33 include_once 'libraries/StringMB.class.php';
34 $this->_byte = new PMA_StringMB();
35 } else {
36 include_once 'libraries/StringNative.class.php';
37 $this->_byte = new PMA_StringNative();
40 if (@extension_loaded('ctype')) {
41 include_once 'libraries/StringCType.class.php';
42 $this->_type = new PMA_StringCType();
43 } else {
44 include_once 'libraries/StringNativeType.class.php';
45 $this->_type = new PMA_StringNativeType();
49 /**
50 * Checks if a given character position in the string is escaped or not
52 * @param string $string string to check for
53 * @param integer $pos the character to check for
54 * @param integer $start starting position in the string
56 * @return boolean whether the character is escaped or not
58 public function charIsEscaped($string, $pos, $start = 0)
60 $pos = max(intval($pos), 0);
61 $start = max(intval($start), 0);
62 $len = $this->strlen($string);
63 // Base case:
64 // Check for string length or invalid input or special case of input
65 // (pos == $start)
66 if ($pos <= $start || $len <= max($pos, $start)) {
67 return false;
70 $pos--;
71 $escaped = false;
72 while ($pos >= $start && $this->substr($string, $pos, 1) == '\\') {
73 $escaped = !$escaped;
74 $pos--;
75 } // end while
77 return $escaped;
80 /**
81 * Checks if a character is an SQL identifier
83 * @param string $c character to check for
84 * @param boolean $dot_is_valid whether the dot character is valid or not
86 * @return boolean whether the character is an SQL identifier or not
88 public function isSqlIdentifier($c, $dot_is_valid = false)
90 return ($this->isAlnum($c)
91 || ($ord_c = $this->ord($c)) && $ord_c >= 192 && $ord_c != 215 &&
92 $ord_c != 249
93 || $c == '_'
94 || $c == '$'
95 || ($dot_is_valid != false && $c == '.'));
98 /**
99 * Returns length of string depending on current charset.
101 * @param string $string string to count
103 * @return int string length
106 public function strlen($string)
108 return $this->_byte->strlen($string);
112 * Returns substring from string, works depending on current charset.
114 * @param string $string string to count
115 * @param int $start start of substring
116 * @param int $length length of substring
118 * @return string the sub string
120 public function substr($string, $start, $length = 2147483647)
122 return $this->_byte->substr($string, $start, $length);
126 * Returns postion of $needle in $haystack or false if not found
128 * @param string $haystack the string being checked
129 * @param string $needle the string to find in haystack
130 * @param int $offset the search offset
132 * @return integer position of $needle in $haystack or false
134 public function strpos($haystack, $needle, $offset = 0)
136 return $this->_byte->strpos($haystack, $needle, $offset);
140 * Make a string lowercase
142 * @param string $string the string being lowercased
144 * @return string the lower case string
146 public function strtolower($string)
148 return $this->_byte->strtolower($string);
152 * Get the ordinal value of a string
154 * @param string $string the string for which ord is required
156 * @return string the ord value
158 public function ord($string)
160 return $this->_byte->ord($string);
164 * Checks if a character is an alphanumeric one
166 * @param string $c character to check for
168 * @return boolean whether the character is an alphanumeric one or not
170 public function isAlnum($c)
172 return $this->_type->isAlnum($c);
176 * Checks if a character is an alphabetic one
178 * @param string $c character to check for
180 * @return boolean whether the character is an alphabetic one or not
182 public function isAlpha($c)
184 return $this->_type->isAlpha($c);
188 * Checks if a character is a digit
190 * @param string $c character to check for
192 * @return boolean whether the character is a digit or not
194 public function isDigit($c)
196 return $this->_type->isDigit($c);
200 * Checks if a character is an upper alphabetic one
202 * @param string $c character to check for
204 * @return boolean whether the character is an upper alphabetic one or not
206 public function isUpper($c)
208 return $this->_type->isUpper($c);
212 * Checks if a character is a lower alphabetic one
214 * @param string $c character to check for
216 * @return boolean whether the character is a lower alphabetic one or not
218 public function isLower($c)
220 return $this->_type->isLower($c);
224 * Checks if a character is a space one
226 * @param string $c character to check for
228 * @return boolean whether the character is a space one or not
230 public function isSpace($c)
232 return $this->_type->isSpace($c);
236 * Checks if a character is an hexadecimal digit
238 * @param string $c character to check for
240 * @return boolean whether the character is an hexadecimal digit or not
242 public function isHexDigit($c)
244 return $this->_type->isHexDigit($c);
248 * Checks if a number is in a range
250 * @param integer $num number to check for
251 * @param integer $lower lower bound
252 * @param integer $upper upper bound
254 * @return boolean whether the number is in the range or not
256 public function numberInRangeInclusive($num, $lower, $upper)
258 return $this->_type->numberInRangeInclusive($num, $lower, $upper);