PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/inc/String.php

http://github.com/thorsten/phpMyFAQ
PHP | 382 lines | 117 code | 42 blank | 223 comment | 5 complexity | 1621dffba4a083a763b88fc481f2fea0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * The main string wrapper class.
  4. *
  5. * PHP Version 5.3.0
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public License,
  8. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. * obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. * @category phpMyFAQ
  12. * @package String
  13. * @author Anatoliy Belsky <ab@php.net>
  14. * @copyright 2009-2012 phpMyFAQ Team
  15. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  16. * @link http://www.phpmyfaq.de
  17. * @since 2009-04-06
  18. */
  19. if (!defined('IS_VALID_PHPMYFAQ')) {
  20. exit();
  21. }
  22. /**
  23. * String
  24. *
  25. * The class uses mbstring extension if available. It's strongly recommended
  26. * to use and extend this class instead of using direct string functions. Doing so
  27. * you garantee your code is upwards compatible with UTF-8 improvements. All
  28. * the string methods behaviour is identical to that of the same named
  29. * single byte string functions.
  30. *
  31. * @category phpMyFAQ
  32. * @package String
  33. * @author Anatoliy Belsky <ab@php.net>
  34. * @copyright 2009-2012 phpMyFAQ Team
  35. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  36. * @link http://www.phpmyfaq.de
  37. * @since 2009-04-06
  38. */
  39. class PMF_String
  40. {
  41. /**
  42. * Instance
  43. *
  44. * @var PMF_String
  45. */
  46. private static $instance;
  47. /**
  48. * Constructor
  49. *
  50. * @return void
  51. */
  52. private final function __construct()
  53. {
  54. }
  55. /**
  56. * Initalize myself
  57. *
  58. * @param string $language Language
  59. *
  60. * @return void
  61. */
  62. public static function init($language = 'en')
  63. {
  64. if (!self::$instance) {
  65. if (extension_loaded('mbstring') && function_exists('mb_regex_encoding')) {
  66. self::$instance = PMF_String_Mbstring::getInstance($language);
  67. } elseif(self::isLangUTF8ToLatinConvertable($language)) {
  68. self::$instance = PMF_String_UTF8ToLatinConvertable::getInstance($language);
  69. } else {
  70. self::$instance = PMF_String_Basic::getInstance($language);
  71. }
  72. }
  73. }
  74. /**
  75. * Get current encoding
  76. *
  77. * @return string
  78. */
  79. public static function getEncoding()
  80. {
  81. return self::$instance->getEncoding();
  82. }
  83. /**
  84. * Get string character count
  85. *
  86. * @param string $str String
  87. *
  88. * @return int
  89. */
  90. public static function strlen($str)
  91. {
  92. return self::$instance->strlen($str);
  93. }
  94. /**
  95. * Get a part of string
  96. *
  97. * @param string $str String
  98. * @param integer $start Start
  99. * @param integer $length Length
  100. *
  101. * @return string
  102. */
  103. public static function substr($string, $start, $length = null)
  104. {
  105. return self::$instance->substr($string, $start, $length);
  106. }
  107. /**
  108. * Get position of the first occurence of a string
  109. *
  110. * @param string $haystack Haystack
  111. * @param string $needle Needle
  112. * @param string $offset Offset
  113. *
  114. * @return int
  115. */
  116. public static function strpos($haystack, $needle, $offset = 0)
  117. {
  118. return self::$instance->strpos($haystack, $needle, $offset);
  119. }
  120. /**
  121. * Make a string lower case
  122. *
  123. * @param string $str String
  124. *
  125. * @return string
  126. */
  127. public static function strtolower($str)
  128. {
  129. return self::$instance->strtolower($str);
  130. }
  131. /**
  132. * Make a string upper case
  133. *
  134. * @param string $str String
  135. *
  136. * @return string
  137. */
  138. public static function strtoupper($str)
  139. {
  140. return self::$instance->strtoupper($str);
  141. }
  142. /**
  143. * Get occurence of a string within another
  144. *
  145. * @param string $haystack Haystack
  146. * @param string $needle Needle
  147. * @param boolean $part Part
  148. *
  149. * @return string|false
  150. */
  151. public static function strstr($haystack, $needle, $part = false)
  152. {
  153. return self::$instance->strstr($haystack, $needle, $part);
  154. }
  155. /**
  156. * Set current encoding
  157. *
  158. * @return string
  159. */
  160. public static function setEncoding($encoding)
  161. {
  162. self::$instance->setEncoding($encoding);
  163. }
  164. /**
  165. * Check if a language could be converted to iso-8859-1
  166. *
  167. * @param string $language
  168. *
  169. * @return boolean
  170. */
  171. public static function isLangUTF8ToLatinConvertable($language)
  172. {
  173. $iso_languages = array('af', 'sq', 'br', 'ca', 'da', 'en', 'fo', 'gl', 'de', 'is', 'it',
  174. 'ku', 'la', 'lb', 'nb', 'oc', 'pt', 'es', 'sw', 'sv', 'wa', 'eu',
  175. // NOTE this languages are not fully supported by latin1
  176. 'nl', 'fr', 'et', 'fi', 'cy'
  177. );
  178. return in_array($language, $iso_languages);
  179. }
  180. /**
  181. * Get last occurence of a string within another
  182. *
  183. * @param string $haystack
  184. * @param string $needle
  185. *
  186. * @return string
  187. */
  188. public static function strrchr($haystack, $needle)
  189. {
  190. return self::$instance->strrchr($haystack, $needle);
  191. }
  192. /**
  193. * Count substring occurences
  194. *
  195. * @param string $haystack
  196. * @param string $needle
  197. *
  198. * @return int
  199. */
  200. public static function substr_count($haystack, $needle)
  201. {
  202. return self::$instance->substr_count($haystack, $needle);
  203. }
  204. /**
  205. * Find position of last occurrence of a char in a string
  206. *
  207. * @param string $haystack
  208. * @param string $needle
  209. * @param int $offset
  210. *
  211. * @return int
  212. */
  213. public static function strrpos($haystack, $needle, $offset = 0)
  214. {
  215. return self::$instance->strrpos($haystack, $needle, $offset);
  216. }
  217. /**
  218. * Match a regexp
  219. *
  220. * @param string $pattern
  221. * @param string $subject
  222. * @param array &$matches
  223. * @param int $flags
  224. * @param int $offset
  225. *
  226. * @return int
  227. */
  228. public static function preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0)
  229. {
  230. return self::$instance->preg_match($pattern, $subject, $matches, $flags, $offset);
  231. }
  232. /**
  233. * Match a regexp globally
  234. *
  235. * @param string $pattern
  236. * @param string $subject
  237. * @param array &$matches
  238. * @param int $flags
  239. * @param int $offset
  240. *
  241. * @return int
  242. */
  243. public static function preg_match_all($pattern, $subject, &$matches = null, $flags = 0, $offset = 0)
  244. {
  245. return self::$instance->preg_match_all($pattern, $subject, $matches, $flags, $offset);
  246. }
  247. /**
  248. * Split string by a regexp
  249. *
  250. * @param string $pattern
  251. * @param string $subject
  252. * @param int $limit
  253. * @param int $flags
  254. *
  255. * @return array
  256. */
  257. public static function preg_split($pattern, $subject, $limit = -1, $flags = 0)
  258. {
  259. return self::$instance->preg_split($pattern, $subject, $limit = -1, $flags = 0);
  260. }
  261. /**
  262. * Search and replace by a regexp using a callback
  263. *
  264. * @param string|array $pattern
  265. * @param function $callback
  266. * @param string|array $subject
  267. * @param int $limit
  268. * @param int &$count
  269. *
  270. * @return array|string
  271. */
  272. public static function preg_replace_callback($pattern, $callback, $subject, $limit= -1, &$count = 0)
  273. {
  274. return self::$instance->preg_replace_callback($pattern, $callback, $subject, $limit, $count);
  275. }
  276. /**
  277. * Search and replace by a regexp
  278. *
  279. * @param string|array $pattern
  280. * @param string|array $replacement
  281. * @param string|array $subject
  282. * @param int $limit
  283. * @param int &$count
  284. *
  285. * @return array|string|null
  286. */
  287. public static function preg_replace($pattern, $replacement, $subject, $limit= -1, &$count = 0)
  288. {
  289. return self::$instance->preg_replace($pattern, $replacement, $subject, $limit, $count);
  290. }
  291. /**
  292. * Check if the string is a unicode string
  293. *
  294. * @param string $str String
  295. *
  296. * @return boolean
  297. */
  298. public static function isUTF8($str)
  299. {
  300. return PMF_String_Basic::isUTF8($str);
  301. }
  302. /**
  303. * Convert special chars to html entities
  304. *
  305. * @param string $string The input string.
  306. * @param integer $quoteStyle Quote style
  307. * @param string $charset Character set, UTF-8 by default
  308. * @param boolean $doubleEncode If set to false, no encoding of existing entities
  309. *
  310. * @return string
  311. */
  312. public static function htmlspecialchars($string, $quoteStyle = ENT_COMPAT, $charset = 'utf-8', $doubleEncode = false)
  313. {
  314. return htmlspecialchars(
  315. $string,
  316. $quoteStyle,
  317. $charset,
  318. $doubleEncode
  319. );
  320. }
  321. /**
  322. * Convert all applicable characters to HTML entities
  323. *
  324. * @param string $string The input string.
  325. * @param integer $quoteStyle Quote style
  326. * @param string $charset Character set, UTF-8 by default
  327. * @param boolean $doubleEncode If set to false, no encoding of existing entities
  328. *
  329. * @return string
  330. */
  331. public static function htmlentities($string, $quoteStyle = ENT_COMPAT, $charset = 'utf-8', $doubleEncode = true)
  332. {
  333. return htmlentities(
  334. $string,
  335. $quoteStyle,
  336. $charset,
  337. $doubleEncode
  338. );
  339. }
  340. }