PageRenderTime 24ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/utilities/string.php

https://bitbucket.org/StasPiv/playzone
PHP | 445 lines | 145 code | 25 blank | 275 comment | 31 complexity | e834859927c1f6d3164a6497f9a6f9aa MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: string.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Utilities
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * PHP mbstring and iconv local configuration
  18. */
  19. // check if mbstring extension is loaded and attempt to load it if not present except for windows
  20. if (extension_loaded('mbstring') || ((!strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && dl('mbstring.so')))) {
  21. //Make sure to surpress the output in case ini_set is disabled
  22. @ini_set('mbstring.internal_encoding', 'UTF-8');
  23. @ini_set('mbstring.http_input', 'UTF-8');
  24. @ini_set('mbstring.http_output', 'UTF-8');
  25. }
  26. // same for iconv
  27. if (function_exists('iconv') || ((!strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && dl('iconv.so')))) {
  28. // these are settings that can be set inside code
  29. iconv_set_encoding("internal_encoding", "UTF-8");
  30. iconv_set_encoding("input_encoding", "UTF-8");
  31. iconv_set_encoding("output_encoding", "UTF-8");
  32. }
  33. /**
  34. * Include the utf8 package
  35. */
  36. require_once(JPATH_LIBRARIES.DS.'phputf8'.DS.'utf8.php');
  37. /**
  38. * String handling class for utf-8 data
  39. * Wraps the phputf8 library
  40. * All functions assume the validity of utf-8 strings.
  41. *
  42. * @static
  43. * @package Joomla.Framework
  44. * @subpackage Utilities
  45. * @since 1.5
  46. */
  47. class JString
  48. {
  49. /**
  50. * UTF-8 aware alternative to strpos
  51. * Find position of first occurrence of a string
  52. *
  53. * @static
  54. * @access public
  55. * @param $str - string String being examined
  56. * @param $search - string String being searced for
  57. * @param $offset - int Optional, specifies the position from which the search should be performed
  58. * @return mixed Number of characters before the first match or FALSE on failure
  59. * @see http://www.php.net/strpos
  60. */
  61. function strpos($str, $search, $offset = FALSE)
  62. {
  63. if ( $offset === FALSE ) {
  64. return utf8_strpos($str, $search);
  65. } else {
  66. return utf8_strpos($str, $search, $offset);
  67. }
  68. }
  69. /**
  70. * UTF-8 aware alternative to strrpos
  71. * Finds position of last occurrence of a string
  72. *
  73. * @static
  74. * @access public
  75. * @param $str - string String being examined
  76. * @param $search - string String being searced for
  77. * @return mixed Number of characters before the last match or FALSE on failure
  78. * @see http://www.php.net/strrpos
  79. */
  80. function strrpos($str, $search){
  81. return utf8_strrpos($str, $search);
  82. }
  83. /**
  84. * UTF-8 aware alternative to substr
  85. * Return part of a string given character offset (and optionally length)
  86. *
  87. * @static
  88. * @access public
  89. * @param string
  90. * @param integer number of UTF-8 characters offset (from left)
  91. * @param integer (optional) length in UTF-8 characters from offset
  92. * @return mixed string or FALSE if failure
  93. * @see http://www.php.net/substr
  94. */
  95. function substr($str, $offset, $length = FALSE)
  96. {
  97. if ( $length === FALSE ) {
  98. return utf8_substr($str, $offset);
  99. } else {
  100. return utf8_substr($str, $offset, $length);
  101. }
  102. }
  103. /**
  104. * UTF-8 aware alternative to strtlower
  105. * Make a string lowercase
  106. * Note: The concept of a characters "case" only exists is some alphabets
  107. * such as Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does
  108. * not exist in the Chinese alphabet, for example. See Unicode Standard
  109. * Annex #21: Case Mappings
  110. *
  111. * @access public
  112. * @param string
  113. * @return mixed either string in lowercase or FALSE is UTF-8 invalid
  114. * @see http://www.php.net/strtolower
  115. */
  116. function strtolower($str){
  117. return utf8_strtolower($str);
  118. }
  119. /**
  120. * UTF-8 aware alternative to strtoupper
  121. * Make a string uppercase
  122. * Note: The concept of a characters "case" only exists is some alphabets
  123. * such as Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does
  124. * not exist in the Chinese alphabet, for example. See Unicode Standard
  125. * Annex #21: Case Mappings
  126. *
  127. * @access public
  128. * @param string
  129. * @return mixed either string in uppercase or FALSE is UTF-8 invalid
  130. * @see http://www.php.net/strtoupper
  131. */
  132. function strtoupper($str){
  133. return utf8_strtoupper($str);
  134. }
  135. /**
  136. * UTF-8 aware alternative to strlen
  137. * Returns the number of characters in the string (NOT THE NUMBER OF BYTES),
  138. *
  139. * @access public
  140. * @param string UTF-8 string
  141. * @return int number of UTF-8 characters in string
  142. * @see http://www.php.net/strlen
  143. */
  144. function strlen($str){
  145. return utf8_strlen($str);
  146. }
  147. /**
  148. * UTF-8 aware alternative to str_ireplace
  149. * Case-insensitive version of str_replace
  150. *
  151. * @static
  152. * @access public
  153. * @param string string to search
  154. * @param string existing string to replace
  155. * @param string new string to replace with
  156. * @param int optional count value to be passed by referene
  157. * @see http://www.php.net/str_ireplace
  158. */
  159. function str_ireplace($search, $replace, $str, $count = NULL)
  160. {
  161. jimport('phputf8.str_ireplace');
  162. if ( $count === FALSE ) {
  163. return utf8_ireplace($search, $replace, $str);
  164. } else {
  165. return utf8_ireplace($search, $replace, $str, $count);
  166. }
  167. }
  168. /**
  169. * UTF-8 aware alternative to str_split
  170. * Convert a string to an array
  171. *
  172. * @static
  173. * @access public
  174. * @param string UTF-8 encoded
  175. * @param int number to characters to split string by
  176. * @return array
  177. * @see http://www.php.net/str_split
  178. */
  179. function str_split($str, $split_len = 1)
  180. {
  181. jimport('phputf8.str_split');
  182. return utf8_str_split($str, $split_len);
  183. }
  184. /**
  185. * UTF-8 aware alternative to strcasecmp
  186. * A case insensivite string comparison
  187. *
  188. * @static
  189. * @access public
  190. * @param string string 1 to compare
  191. * @param string string 2 to compare
  192. * @return int < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
  193. * @see http://www.php.net/strcasecmp
  194. */
  195. function strcasecmp($str1, $str2)
  196. {
  197. jimport('phputf8.strcasecmp');
  198. return utf8_strcasecmp($str1, $str2);
  199. }
  200. /**
  201. * UTF-8 aware alternative to strcspn
  202. * Find length of initial segment not matching mask
  203. *
  204. * @static
  205. * @access public
  206. * @param string
  207. * @param string the mask
  208. * @param int Optional starting character position (in characters)
  209. * @param int Optional length
  210. * @return int the length of the initial segment of str1 which does not contain any of the characters in str2
  211. * @see http://www.php.net/strcspn
  212. */
  213. function strcspn($str, $mask, $start = NULL, $length = NULL)
  214. {
  215. jimport('phputf8.strcspn');
  216. if ( $start === FALSE && $length === FALSE ) {
  217. return utf8_strcspn($str, $mask);
  218. } else if ( $length === FALSE ) {
  219. return utf8_strcspn($str, $mask, $start);
  220. } else {
  221. return utf8_strcspn($str, $mask, $start, $length);
  222. }
  223. }
  224. /**
  225. * UTF-8 aware alternative to stristr
  226. * Returns all of haystack from the first occurrence of needle to the end.
  227. * needle and haystack are examined in a case-insensitive manner
  228. * Find first occurrence of a string using case insensitive comparison
  229. *
  230. * @static
  231. * @access public
  232. * @param string the haystack
  233. * @param string the needle
  234. * @return string the sub string
  235. * @see http://www.php.net/stristr
  236. */
  237. function stristr($str, $search)
  238. {
  239. jimport('phputf8.stristr');
  240. return utf8_stristr($str, $search);
  241. }
  242. /**
  243. * UTF-8 aware alternative to strrev
  244. * Reverse a string
  245. *
  246. * @static
  247. * @access public
  248. * @param string String to be reversed
  249. * @return string The string in reverse character order
  250. * @see http://www.php.net/strrev
  251. */
  252. function strrev($str)
  253. {
  254. jimport('phputf8.strrev');
  255. return utf8_strrev($str);
  256. }
  257. /**
  258. * UTF-8 aware alternative to strspn
  259. * Find length of initial segment matching mask
  260. *
  261. * @static
  262. * @access public
  263. * @param string the haystack
  264. * @param string the mask
  265. * @param int start optional
  266. * @param int length optional
  267. * @see http://www.php.net/strspn
  268. */
  269. function strspn($str, $mask, $start = NULL, $length = NULL)
  270. {
  271. jimport('phputf8.strspn');
  272. if ( $start === FALSE && $length === FALSE ) {
  273. return utf8_strspn($str, $mask);
  274. } else if ( $length === FALSE ) {
  275. return utf8_strspn($str, $mask, $start);
  276. } else {
  277. return utf8_strspn($str, $mask, $start, $length);
  278. }
  279. }
  280. /**
  281. * UTF-8 aware substr_replace
  282. * Replace text within a portion of a string
  283. *
  284. * @static
  285. * @access public
  286. * @param string the haystack
  287. * @param string the replacement string
  288. * @param int start
  289. * @param int length (optional)
  290. * @see http://www.php.net/substr_replace
  291. */
  292. function substr_replace($str, $repl, $start, $length = NULL )
  293. {
  294. // loaded by library loader
  295. if ( $length === FALSE ) {
  296. return utf8_substr_replace($str, $repl, $start);
  297. } else {
  298. return utf8_substr_replace($str, $repl, $start, $length);
  299. }
  300. }
  301. /**
  302. * UTF-8 aware replacement for ltrim()
  303. * Strip whitespace (or other characters) from the beginning of a string
  304. * Note: you only need to use this if you are supplying the charlist
  305. * optional arg and it contains UTF-8 characters. Otherwise ltrim will
  306. * work normally on a UTF-8 string
  307. *
  308. * @static
  309. * @access public
  310. * @param string the string to be trimmed
  311. * @param string the optional charlist of additional characters to trim
  312. * @return string the trimmed string
  313. * @see http://www.php.net/ltrim
  314. */
  315. function ltrim( $str, $charlist = FALSE )
  316. {
  317. jimport('phputf8.trim');
  318. if ( $charlist === FALSE ) {
  319. return utf8_ltrim( $str );
  320. } else {
  321. return utf8_ltrim( $str, $charlist );
  322. }
  323. }
  324. /**
  325. * UTF-8 aware replacement for rtrim()
  326. * Strip whitespace (or other characters) from the end of a string
  327. * Note: you only need to use this if you are supplying the charlist
  328. * optional arg and it contains UTF-8 characters. Otherwise rtrim will
  329. * work normally on a UTF-8 string
  330. *
  331. * @static
  332. * @access public
  333. * @param string the string to be trimmed
  334. * @param string the optional charlist of additional characters to trim
  335. * @return string the trimmed string
  336. * @see http://www.php.net/rtrim
  337. */
  338. function rtrim( $str, $charlist = FALSE )
  339. {
  340. jimport('phputf8.trim');
  341. if ( $charlist === FALSE ) {
  342. return utf8_rtrim($str);
  343. } else {
  344. return utf8_rtrim( $str, $charlist );
  345. }
  346. }
  347. /**
  348. * UTF-8 aware replacement for trim()
  349. * Strip whitespace (or other characters) from the beginning and end of a string
  350. * Note: you only need to use this if you are supplying the charlist
  351. * optional arg and it contains UTF-8 characters. Otherwise trim will
  352. * work normally on a UTF-8 string
  353. *
  354. * @static
  355. * @access public
  356. * @param string the string to be trimmed
  357. * @param string the optional charlist of additional characters to trim
  358. * @return string the trimmed string
  359. * @see http://www.php.net/trim
  360. */
  361. function trim( $str, $charlist = FALSE )
  362. {
  363. jimport('phputf8.trim');
  364. if ( $charlist === FALSE ) {
  365. return utf8_trim( $str );
  366. } else {
  367. return utf8_trim( $str, $charlist );
  368. }
  369. }
  370. /**
  371. * UTF-8 aware alternative to ucfirst
  372. * Make a string's first character uppercase
  373. *
  374. * @static
  375. * @access public
  376. * @param string
  377. * @return string with first character as upper case (if applicable)
  378. * @see http://www.php.net/ucfirst
  379. */
  380. function ucfirst($str)
  381. {
  382. jimport('phputf8.ucfirst');
  383. return utf8_ucfirst($str);
  384. }
  385. /**
  386. * UTF-8 aware alternative to ucwords
  387. * Uppercase the first character of each word in a string
  388. *
  389. * @static
  390. * @access public
  391. * @param string
  392. * @return string with first char of each word uppercase
  393. * @see http://www.php.net/ucwords
  394. */
  395. function ucwords($str)
  396. {
  397. jimport('phputf8.ucwords');
  398. return utf8_ucwords($str);
  399. }
  400. /**
  401. * Transcode a string.
  402. *
  403. * @static
  404. * @param string $source The string to transcode.
  405. * @param string $from_encoding The source encoding.
  406. * @param string $to_encoding The target encoding.
  407. * @return string Transcoded string
  408. * @since 1.5
  409. */
  410. function transcode($source, $from_encoding, $to_encoding) {
  411. if (is_string($source)) {
  412. /*
  413. * "//TRANSLIT" is appendd to the $to_encoding to ensure that when iconv comes
  414. * across a character that cannot be represented in the target charset, it can
  415. * be approximated through one or several similarly looking characters.
  416. */
  417. return iconv($from_encoding, $to_encoding.'//TRANSLIT', $source);
  418. }
  419. }
  420. }