PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/joomla/utilities/string.php

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