Source for file String.php

Documentation is available at String.php

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