PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/www/libs/nette-dev/String.php

https://github.com/bazo/Mokuji
PHP | 272 lines | 101 code | 57 blank | 114 comment | 6 complexity | 799c94d30c6d6fa45b4afcb94702209b MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette
  10. */
  11. /**
  12. * String tools library.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette
  16. */
  17. final class String
  18. {
  19. /**
  20. * Static class - cannot be instantiated.
  21. */
  22. final public function __construct()
  23. {
  24. throw new LogicException("Cannot instantiate static class " . get_class($this));
  25. }
  26. /**
  27. * Checks if the string is valid for the specified encoding.
  28. * @param string byte stream to check
  29. * @param string expected encoding
  30. * @return bool
  31. */
  32. public static function checkEncoding($s, $encoding = 'UTF-8')
  33. {
  34. return $s === self::fixEncoding($s, $encoding);
  35. }
  36. /**
  37. * Returns correctly encoded string.
  38. * @param string byte stream to fix
  39. * @param string encoding
  40. * @return string
  41. */
  42. public static function fixEncoding($s, $encoding = 'UTF-8')
  43. {
  44. // removes xD800-xDFFF, xFEFF, xFFFF, x110000 and higher
  45. return @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s)); // intentionally @
  46. }
  47. /**
  48. * Returns a specific character.
  49. * @param int codepoint
  50. * @param string encoding
  51. * @return string
  52. */
  53. public static function chr($code, $encoding = 'UTF-8')
  54. {
  55. return iconv('UTF-32BE', $encoding . '//IGNORE', pack('N', $code));
  56. }
  57. /**
  58. * Starts the $haystack string with the prefix $needle?
  59. * @param string
  60. * @param string
  61. * @return bool
  62. */
  63. public static function startsWith($haystack, $needle)
  64. {
  65. return strncmp($haystack, $needle, strlen($needle)) === 0;
  66. }
  67. /**
  68. * Ends the $haystack string with the suffix $needle?
  69. * @param string
  70. * @param string
  71. * @return bool
  72. */
  73. public static function endsWith($haystack, $needle)
  74. {
  75. return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle;
  76. }
  77. /**
  78. * Removes special controls characters and normalizes line endings and spaces.
  79. * @param string UTF-8 encoding or 8-bit
  80. * @return string
  81. */
  82. public static function normalize($s)
  83. {
  84. // standardize line endings to unix-like
  85. $s = str_replace("\r\n", "\n", $s); // DOS
  86. $s = strtr($s, "\r", "\n"); // Mac
  87. // remove control characters; leave \t + \n
  88. $s = preg_replace('#[\x00-\x08\x0B-\x1F]+#', '', $s);
  89. // right trim
  90. $s = preg_replace("#[\t ]+$#m", '', $s);
  91. // trailing spaces
  92. $s = trim($s, "\n");
  93. return $s;
  94. }
  95. /**
  96. * Converts to web safe characters [a-z0-9-] text.
  97. * @param string UTF-8 encoding
  98. * @param string ASCII
  99. * @param bool
  100. * @return string
  101. */
  102. public static function webalize($s, $charlist = NULL, $lower = TRUE)
  103. {
  104. $s = strtr($s, '`\'"^~', '-----');
  105. if (ICONV_IMPL === 'glibc') {
  106. $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); // intentionally @
  107. $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"
  108. ."\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",
  109. "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
  110. } else {
  111. $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); // intentionally @
  112. }
  113. $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
  114. if ($lower) $s = strtolower($s);
  115. $s = preg_replace('#[^a-z0-9' . preg_quote($charlist, '#') . ']+#i', '-', $s);
  116. $s = trim($s, '-');
  117. return $s;
  118. }
  119. /**
  120. * Truncates string to maximal length.
  121. * @param string UTF-8 encoding
  122. * @param int
  123. * @param string UTF-8 encoding
  124. * @return string
  125. */
  126. public static function truncate($s, $maxLen, $append = "\xE2\x80\xA6")
  127. {
  128. if (iconv_strlen($s, 'UTF-8') > $maxLen) {
  129. $maxLen = $maxLen - iconv_strlen($append, 'UTF-8');
  130. if ($maxLen < 1) {
  131. return $append;
  132. } elseif (preg_match('#^.{1,'.$maxLen.'}(?=[\s\x00-@\[-`{-~])#us', $s, $matches)) {
  133. return $matches[0] . $append;
  134. } else {
  135. return iconv_substr($s, 0, $maxLen, 'UTF-8') . $append;
  136. }
  137. }
  138. return $s;
  139. }
  140. /**
  141. * Indents the content from the left.
  142. * @param string UTF-8 encoding or 8-bit
  143. * @param int
  144. * @param string
  145. * @return string
  146. */
  147. public static function indent($s, $level = 1, $chars = "\t")
  148. {
  149. return $level < 1 ? $s : preg_replace('#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . str_repeat($chars, $level), $s);
  150. }
  151. /**
  152. * Convert to lower case.
  153. * @param string UTF-8 encoding
  154. * @return string
  155. */
  156. public static function lower($s)
  157. {
  158. return mb_strtolower($s, 'UTF-8');
  159. }
  160. /**
  161. * Convert to upper case.
  162. * @param string UTF-8 encoding
  163. * @return string
  164. */
  165. public static function upper($s)
  166. {
  167. return mb_strtoupper($s, 'UTF-8');
  168. }
  169. /**
  170. * Capitalize string.
  171. * @param string UTF-8 encoding
  172. * @return string
  173. */
  174. public static function capitalize($s)
  175. {
  176. return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8');
  177. }
  178. /**
  179. * Strips whitespace.
  180. * @param string UTF-8 encoding
  181. * @param string
  182. * @return string
  183. */
  184. public static function trim($s, $charlist = " \t\n\r\0\x0B\xC2\xA0")
  185. {
  186. $charlist = preg_quote($charlist, '#');
  187. return preg_replace('#^['.$charlist.']+|['.$charlist.']+$#u', '', $s);
  188. }
  189. /**
  190. * Pad a string to a certain length with another string.
  191. * @param string UTF-8 encoding
  192. * @param int
  193. * @param string
  194. * @return string
  195. */
  196. public static function padLeft($s, $length, $pad = ' ')
  197. {
  198. $length = max(0, $length - iconv_strlen($s, 'UTF-8'));
  199. $padLen = iconv_strlen($pad, 'UTF-8');
  200. return str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8') . $s;
  201. }
  202. /**
  203. * Pad a string to a certain length with another string.
  204. * @param string UTF-8 encoding
  205. * @param int
  206. * @param string
  207. * @return string
  208. */
  209. public static function padRight($s, $length, $pad = ' ')
  210. {
  211. $length = max(0, $length - iconv_strlen($s, 'UTF-8'));
  212. $padLen = iconv_strlen($pad, 'UTF-8');
  213. return $s . str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8');
  214. }
  215. }