PageRenderTime 66ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/tools/String.php

https://github.com/jakubkinst/Course-Manager
PHP | 474 lines | 219 code | 89 blank | 166 comment | 19 complexity | 7ffd76bb9f3eb73a90e3b15bab3380fe MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (http://nette.org)
  4. *
  5. * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
  6. *
  7. * For the full copyright and license information, please view
  8. * the file license.txt that was distributed with this source code.
  9. * @package Nette
  10. */
  11. /**
  12. * String tools library.
  13. *
  14. * @author David Grudl
  15. */
  16. final class String
  17. {
  18. /**
  19. * Static class - cannot be instantiated.
  20. */
  21. final public function __construct()
  22. {
  23. throw new LogicException("Cannot instantiate static class " . get_class($this));
  24. }
  25. /**
  26. * Checks if the string is valid for the specified encoding.
  27. * @param string byte stream to check
  28. * @param string expected encoding
  29. * @return bool
  30. */
  31. public static function checkEncoding($s, $encoding = 'UTF-8')
  32. {
  33. return $s === self::fixEncoding($s, $encoding);
  34. }
  35. /**
  36. * Returns correctly encoded string.
  37. * @param string byte stream to fix
  38. * @param string encoding
  39. * @return string
  40. */
  41. public static function fixEncoding($s, $encoding = 'UTF-8')
  42. {
  43. // removes xD800-xDFFF, xFEFF, xFFFF, x110000 and higher
  44. return @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s)); // intentionally @
  45. }
  46. /**
  47. * Returns a specific character.
  48. * @param int codepoint
  49. * @param string encoding
  50. * @return string
  51. */
  52. public static function chr($code, $encoding = 'UTF-8')
  53. {
  54. return iconv('UTF-32BE', $encoding . '//IGNORE', pack('N', $code));
  55. }
  56. /**
  57. * Starts the $haystack string with the prefix $needle?
  58. * @param string
  59. * @param string
  60. * @return bool
  61. */
  62. public static function startsWith($haystack, $needle)
  63. {
  64. return strncmp($haystack, $needle, strlen($needle)) === 0;
  65. }
  66. /**
  67. * Ends the $haystack string with the suffix $needle?
  68. * @param string
  69. * @param string
  70. * @return bool
  71. */
  72. public static function endsWith($haystack, $needle)
  73. {
  74. return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle;
  75. }
  76. /**
  77. * Removes special controls characters and normalizes line endings and spaces.
  78. * @param string UTF-8 encoding or 8-bit
  79. * @return string
  80. */
  81. public static function normalize($s)
  82. {
  83. // standardize line endings to unix-like
  84. $s = str_replace("\r\n", "\n", $s); // DOS
  85. $s = strtr($s, "\r", "\n"); // Mac
  86. // remove control characters; leave \t + \n
  87. $s = preg_replace('#[\x00-\x08\x0B-\x1F]+#', '', $s);
  88. // right trim
  89. $s = preg_replace("#[\t ]+$#m", '', $s);
  90. // trailing spaces
  91. $s = trim($s, "\n");
  92. return $s;
  93. }
  94. /**
  95. * Converts to ASCII.
  96. * @param string UTF-8 encoding
  97. * @return string ASCII
  98. */
  99. public static function toAscii($s)
  100. {
  101. $s = preg_replace('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u', '', $s);
  102. $s = strtr($s, '`\'"^~', "\x01\x02\x03\x04\x05");
  103. if (ICONV_IMPL === 'glibc') {
  104. $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); // intentionally @
  105. $s = strtr($s, "\xa5\xa3\xbc\x8c\xa7\x8a\xaa\x8d\x8f\x8e\xaf\xb9\xb3\xbe\x9c\x9a\xba\x9d\x9f\x9e\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2"
  106. ."\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe",
  107. "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
  108. } else {
  109. $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); // intentionally @
  110. }
  111. $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
  112. return strtr($s, "\x01\x02\x03\x04\x05", '`\'"^~');
  113. }
  114. /**
  115. * Converts to web safe characters [a-z0-9-] text.
  116. * @param string UTF-8 encoding
  117. * @param string allowed characters
  118. * @param bool
  119. * @return string
  120. */
  121. public static function webalize($s, $charlist = NULL, $lower = TRUE)
  122. {
  123. $s = self::toAscii($s);
  124. if ($lower) $s = strtolower($s);
  125. $s = preg_replace('#[^a-z0-9' . preg_quote($charlist, '#') . ']+#i', '-', $s);
  126. $s = trim($s, '-');
  127. return $s;
  128. }
  129. /**
  130. * Truncates string to maximal length.
  131. * @param string UTF-8 encoding
  132. * @param int
  133. * @param string UTF-8 encoding
  134. * @return string
  135. */
  136. public static function truncate($s, $maxLen, $append = "\xE2\x80\xA6")
  137. {
  138. if (self::length($s) > $maxLen) {
  139. $maxLen = $maxLen - self::length($append);
  140. if ($maxLen < 1) {
  141. return $append;
  142. } elseif ($matches = self::match($s, '#^.{1,'.$maxLen.'}(?=[\s\x00-/:-@\[-`{-~])#us')) {
  143. return $matches[0] . $append;
  144. } else {
  145. return iconv_substr($s, 0, $maxLen, 'UTF-8') . $append;
  146. }
  147. }
  148. return $s;
  149. }
  150. /**
  151. * Indents the content from the left.
  152. * @param string UTF-8 encoding or 8-bit
  153. * @param int
  154. * @param string
  155. * @return string
  156. */
  157. public static function indent($s, $level = 1, $chars = "\t")
  158. {
  159. return $level < 1 ? $s : self::replace($s, '#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . str_repeat($chars, $level));
  160. }
  161. /**
  162. * Convert to lower case.
  163. * @param string UTF-8 encoding
  164. * @return string
  165. */
  166. public static function lower($s)
  167. {
  168. return mb_strtolower($s, 'UTF-8');
  169. }
  170. /**
  171. * Convert to upper case.
  172. * @param string UTF-8 encoding
  173. * @return string
  174. */
  175. public static function upper($s)
  176. {
  177. return mb_strtoupper($s, 'UTF-8');
  178. }
  179. /**
  180. * Convert first character to upper case.
  181. * @param string UTF-8 encoding
  182. * @return string
  183. */
  184. public static function firstUpper($s)
  185. {
  186. return self::upper(mb_substr($s, 0, 1, 'UTF-8')) . mb_substr($s, 1, self::length($s), 'UTF-8');
  187. }
  188. /**
  189. * Capitalize string.
  190. * @param string UTF-8 encoding
  191. * @return string
  192. */
  193. public static function capitalize($s)
  194. {
  195. return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8');
  196. }
  197. /**
  198. * Case-insensitive compares UTF-8 strings.
  199. * @param string
  200. * @param string
  201. * @param int
  202. * @return bool
  203. */
  204. public static function compare($left, $right, $len = NULL)
  205. {
  206. if ($len < 0) {
  207. $left = iconv_substr($left, $len, -$len, 'UTF-8');
  208. $right = iconv_substr($right, $len, -$len, 'UTF-8');
  209. } elseif ($len !== NULL) {
  210. $left = iconv_substr($left, 0, $len, 'UTF-8');
  211. $right = iconv_substr($right, 0, $len, 'UTF-8');
  212. }
  213. return self::lower($left) === self::lower($right);
  214. }
  215. /**
  216. * Returns UTF-8 string length.
  217. * @param string
  218. * @return int
  219. */
  220. public static function length($s)
  221. {
  222. return function_exists('mb_strlen') ? mb_strlen($s, 'UTF-8') : strlen(utf8_decode($s));
  223. }
  224. /**
  225. * Strips whitespace.
  226. * @param string UTF-8 encoding
  227. * @param string
  228. * @return string
  229. */
  230. public static function trim($s, $charlist = " \t\n\r\0\x0B\xC2\xA0")
  231. {
  232. $charlist = preg_quote($charlist, '#');
  233. return self::replace($s, '#^['.$charlist.']+|['.$charlist.']+$#u', '');
  234. }
  235. /**
  236. * Pad a string to a certain length with another string.
  237. * @param string UTF-8 encoding
  238. * @param int
  239. * @param string
  240. * @return string
  241. */
  242. public static function padLeft($s, $length, $pad = ' ')
  243. {
  244. $length = max(0, $length - self::length($s));
  245. $padLen = self::length($pad);
  246. return str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8') . $s;
  247. }
  248. /**
  249. * Pad a string to a certain length with another string.
  250. * @param string UTF-8 encoding
  251. * @param int
  252. * @param string
  253. * @return string
  254. */
  255. public static function padRight($s, $length, $pad = ' ')
  256. {
  257. $length = max(0, $length - self::length($s));
  258. $padLen = self::length($pad);
  259. return $s . str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8');
  260. }
  261. /**
  262. * Generate random string.
  263. * @param int
  264. * @param string
  265. * @return string
  266. */
  267. public static function random($length = 10, $charlist = '0-9a-z')
  268. {
  269. $charlist = str_shuffle(preg_replace_callback('#.-.#', create_function('$m', '
  270. return implode(\'\', range($m[0][0], $m[0][2]));
  271. '), $charlist));
  272. $chLen = strlen($charlist);
  273. $s = '';
  274. for ($i = 0; $i < $length; $i++) {
  275. if ($i % 5 === 0) {
  276. $rand = lcg_value();
  277. $rand2 = microtime(TRUE);
  278. }
  279. $rand *= $chLen;
  280. $s .= $charlist[($rand + $rand2) % $chLen];
  281. $rand -= (int) $rand;
  282. }
  283. return $s;
  284. }
  285. /**
  286. * Splits string by a regular expression.
  287. * @param string
  288. * @param string
  289. * @param int
  290. * @return array
  291. */
  292. public static function split($subject, $pattern, $flags = 0)
  293. {
  294. Debug::tryError();
  295. $res = preg_split($pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE);
  296. self::catchPregError($pattern);
  297. return $res;
  298. }
  299. /**
  300. * Performs a regular expression match.
  301. * @param string
  302. * @param string
  303. * @param int
  304. * @param int
  305. * @return mixed
  306. */
  307. public static function match($subject, $pattern, $flags = 0, $offset = 0)
  308. {
  309. Debug::tryError();
  310. $res = preg_match($pattern, $subject, $m, $flags, $offset);
  311. self::catchPregError($pattern);
  312. if ($res) {
  313. return $m;
  314. }
  315. }
  316. /**
  317. * Performs a global regular expression match.
  318. * @param string
  319. * @param string
  320. * @param int (PREG_SET_ORDER is default)
  321. * @param int
  322. * @return array
  323. */
  324. public static function matchAll($subject, $pattern, $flags = 0, $offset = 0)
  325. {
  326. Debug::tryError();
  327. $res = preg_match_all($pattern, $subject, $m, ($flags & PREG_PATTERN_ORDER) ? $flags : ($flags | PREG_SET_ORDER), $offset);
  328. self::catchPregError($pattern);
  329. return $m;
  330. }
  331. /**
  332. * Perform a regular expression search and replace.
  333. * @param string
  334. * @param string|array
  335. * @param string|callback
  336. * @param int
  337. * @return string
  338. */
  339. public static function replace($subject, $pattern, $replacement = NULL, $limit = -1)
  340. {
  341. Debug::tryError();
  342. if (is_object($replacement) || is_array($replacement)) {
  343. if ($replacement instanceof Callback) {
  344. $replacement = $replacement->getNative();
  345. }
  346. if (!is_callable($replacement, FALSE, $textual)) {
  347. Debug::catchError($foo);
  348. throw new InvalidStateException("Callback '$textual' is not callable.");
  349. }
  350. $res = preg_replace_callback($pattern, $replacement, $subject, $limit);
  351. if (Debug::catchError($e)) { // compile error
  352. $trace = $e->getTrace();
  353. if (isset($trace[2]['class']) && $trace[2]['class'] === __CLASS__) {
  354. throw new RegexpException($e->getMessage() . " in pattern: $pattern");
  355. }
  356. }
  357. } elseif (is_array($pattern)) {
  358. $res = preg_replace(array_keys($pattern), array_values($pattern), $subject, $limit);
  359. } else {
  360. $res = preg_replace($pattern, $replacement, $subject, $limit);
  361. }
  362. self::catchPregError($pattern);
  363. return $res;
  364. }
  365. /** @internal */
  366. public static function catchPregError($pattern)
  367. {
  368. if (Debug::catchError($e)) { // compile error
  369. throw new RegexpException($e->getMessage() . " in pattern: $pattern");
  370. } elseif (preg_last_error()) { // run-time error
  371. static $messages = array(
  372. PREG_INTERNAL_ERROR => 'Internal error',
  373. PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
  374. PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
  375. PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
  376. 5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', // PREG_BAD_UTF8_OFFSET_ERROR
  377. );
  378. $code = preg_last_error();
  379. throw new RegexpException((isset($messages[$code]) ? $messages[$code] : 'Unknown error') . " (pattern: $pattern)", $code);
  380. }
  381. }
  382. }