PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyfaq/inc/String/UTF8ToLatinConvertable.php

http://github.com/thorsten/phpMyFAQ
PHP | 351 lines | 108 code | 46 blank | 197 comment | 7 complexity | 0e92479b1260e55a496a7d7153c75195 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * The string wrapper class using single byte string functions.
  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-08
  18. */
  19. if (!defined('IS_VALID_PHPMYFAQ')) {
  20. exit();
  21. }
  22. /**
  23. * PMF_String_UTF8ToLatinConvertable
  24. *
  25. * The trick is to use xml utf8 functions to encode and decode
  26. * utf8 strings. This is useful for strings which could be
  27. * cleanly converted to iso-8859-1 and back with utf8_decode and
  28. * utf8_decode, so then non multibyte functions could be used.
  29. *
  30. * TODO Cover also nearly complete supported charsets, languages and chars
  31. * Notice this article: http://en.wikipedia.org/wiki/ISO_8859-1
  32. *
  33. * @category phpMyFAQ
  34. * @package String
  35. * @author Anatoliy Belsky <ab@php.net>
  36. * @copyright 2009-2012 phpMyFAQ Team
  37. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  38. * @link http://www.phpmyfaq.de
  39. * @since 2009-04-06
  40. */
  41. class PMF_String_UTF8ToLatinConvertable extends PMF_String_Abstract
  42. {
  43. /**
  44. * Instance
  45. *
  46. * @var PMF_String_UTF8ToLatinConvertable
  47. */
  48. private static $instance;
  49. /**
  50. *
  51. * Constructor
  52. *
  53. * @return PMF_String_Basic
  54. */
  55. private final function __construct()
  56. {
  57. }
  58. /**
  59. * Create and return an instance
  60. *
  61. * @param string $language
  62. *
  63. * @return PMF_String_Basic
  64. */
  65. public static function getInstance($language = 'en')
  66. {
  67. if (!self::$instance) {
  68. self::$instance = new self;
  69. self::$instance->encoding = self::DEFAULT_ENCODING;
  70. self::$instance->language = PMF_Language::isASupportedLanguage($language) ? $language : self::DEFAULT_LANGUAGE;
  71. }
  72. return self::$instance;
  73. }
  74. /**
  75. * Prepare a string to be used with a single byte string function.
  76. * If the string isn't utf8, presume it's iso
  77. * @param string $str
  78. *
  79. * @return string
  80. */
  81. public function iso($str)
  82. {
  83. return self::isUTF8($str) ? utf8_decode($str) : $str;
  84. }
  85. /**
  86. * Convert a string back to it's original charset which is utf8
  87. * @param string $str
  88. *
  89. * @return string
  90. */
  91. public function utf8($str)
  92. {
  93. return self::isUTF8($str) ? $str : utf8_encode($str);
  94. }
  95. /**
  96. * Get string character count
  97. *
  98. * @param string $str String
  99. *
  100. * @return int
  101. */
  102. public function strlen($str)
  103. {
  104. return strlen($this->iso($str));
  105. }
  106. /**
  107. * Get a part of string
  108. *
  109. * @param string $str String
  110. * @param int $start Start
  111. * @param int $length Length
  112. *
  113. * @return string
  114. */
  115. public function substr($str, $start, $length = null)
  116. {
  117. $length = null == $length ? $this->strlen($str) : $length;
  118. $retval = substr($this->iso($str), $start, $length);
  119. return $this->utf8($retval);
  120. }
  121. /**
  122. * Get position of the first occurence of a string
  123. *
  124. * @param string $haystack Haystack
  125. * @param string $needle Needle
  126. * @param string $offset Offset
  127. *
  128. * @return int
  129. */
  130. public function strpos($haystack, $needle, $offset = 0)
  131. {
  132. return strpos($this->iso($haystack), $this->iso($needle), $offset);
  133. }
  134. /**
  135. * Make a string lower case
  136. *
  137. * @param string $str String
  138. *
  139. * @return string
  140. */
  141. public function strtolower($str)
  142. {
  143. $retval = strtolower($this->iso($str));
  144. return $this->utf8($retval);
  145. }
  146. /**
  147. * Make a string upper case
  148. *
  149. * @param string $str String
  150. *
  151. * @return string
  152. */
  153. public function strtoupper($str)
  154. {
  155. $retval = strtoupper($this->iso($str));
  156. return $this->utf8($retval);
  157. }
  158. /**
  159. * Get occurence of a string within another
  160. *
  161. * @param string $haystack Haystack
  162. * @param string $needle Needle
  163. * @param boolean $part Part
  164. *
  165. * @return string|false
  166. */
  167. public function strstr($haystack, $needle, $part = false)
  168. {
  169. $retval = strstr($this->iso($haystack), $this->iso($needle), $part);
  170. return $this->utf8($retval);
  171. }
  172. /**
  173. * Get last occurence of a string within another
  174. * @param string $haystack
  175. * @param string $needle
  176. *
  177. * @return string
  178. */
  179. public function strrchr($haystack, $needle)
  180. {
  181. $retval = strrchr($this->iso($haystack), $this->iso($needle));
  182. return $this->utf8($retval);
  183. }
  184. /**
  185. *
  186. * Count substring occurences
  187. * @param string $haystack
  188. * @param string $needle
  189. *
  190. * @return int
  191. */
  192. public function substr_count($haystack, $needle)
  193. {
  194. return substr_count($this->iso($haystack), $this->iso($needle));
  195. }
  196. /**
  197. * Find position of last occurrence of a char in a string
  198. * @param string $haystack
  199. * @param string $needle
  200. * @param int $offset
  201. *
  202. * @return int
  203. */
  204. public function strrpos($haystack, $needle, $offset = 0)
  205. {
  206. return strrpos($this->iso($haystack), $this->iso($needle), $offset);
  207. }
  208. /**
  209. *
  210. * Match a regexp
  211. * @param string $pattern
  212. * @param string $subject
  213. * @param array &$matches
  214. * @param int $flags
  215. * @param int $offset
  216. *
  217. * @return int
  218. */
  219. public function preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0)
  220. {
  221. return preg_match(self::appendU($pattern), $subject, $matches, $flags, $offset);
  222. }
  223. /**
  224. *
  225. * Match a regexp globally
  226. * @param string $pattern
  227. * @param string $subject
  228. * @param array &$matches
  229. * @param int $flags
  230. * @param int $offset
  231. *
  232. * @return int
  233. */
  234. public function preg_match_all($pattern, $subject, &$matches, $flags = 0, $offset = 0)
  235. {
  236. return preg_match_all(self::appendU($pattern), $subject, $matches, $flags, $offset);
  237. }
  238. /**
  239. * Split string by a regexp
  240. * @param string $pattern
  241. * @param string $subject
  242. * @param int $limit
  243. * @param int $flags
  244. *
  245. * @return array
  246. */
  247. public function preg_split($pattern, $subject, $limit = -1, $flags = 0)
  248. {
  249. return preg_split(self::appendU($pattern), $subject, $limit, $flags);
  250. }
  251. /**
  252. * Search and replace by a regexp using a callback
  253. * @param string|array $pattern
  254. * @param function $callback
  255. * @param string|array $subject
  256. * @param int $limit
  257. * @param int &$count
  258. *
  259. * @return array|string
  260. */
  261. public function preg_replace_callback($pattern, $callback, $subject, $limit= -1, &$count = 0)
  262. {
  263. if (is_array($pattern)) {
  264. foreach($pattern as &$p) {
  265. $p = self::appendU($p);
  266. }
  267. } else {
  268. $pattern = self::appendU($pattern);
  269. }
  270. return preg_replace_callback($pattern, $callback, $subject, $limit, $count);
  271. }
  272. /**
  273. * Search and replace by a regexp
  274. * @param string|array $pattern
  275. * @param string|array $replacement
  276. * @param string|array $subject
  277. * @param int $limit
  278. * @param int &$count
  279. *
  280. * @return array|string|null
  281. */
  282. public function preg_replace($pattern, $replacement, $subject, $limit= -1, &$count = 0)
  283. {
  284. if (is_array($pattern)) {
  285. foreach($pattern as &$p) {
  286. $p = self::appendU($p);
  287. }
  288. } else {
  289. $pattern = self::appendU($pattern);
  290. }
  291. return preg_replace($pattern, $replacement, $subject, $limit, $count);
  292. }
  293. /**
  294. * Append an u to the string. The string is supposed
  295. * to be a regex prepared to use with a preg_* function
  296. *
  297. * @param string $str
  298. *
  299. * @return string
  300. */
  301. private static function appendU($str)
  302. {
  303. return ((string) $str) . 'u';
  304. }
  305. }