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