PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/system/classes/helpers/str.php

http://github.com/enormego/EightPHP
PHP | 749 lines | 338 code | 97 blank | 314 comment | 59 complexity | cd3f4eae380f3f6ae0db1fc1c708d03b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * String helper class.
  4. *
  5. * @package System
  6. * @subpackage Helpers
  7. * @author EightPHP Development Team
  8. * @copyright (c) 2009-2010 EightPHP
  9. * @license http://license.eightphp.com
  10. */
  11. class str_Core {
  12. /**
  13. * Limits a phrase to a given number of words.
  14. *
  15. * @param string phrase to limit words of
  16. * @param integer number of words to limit to
  17. * @param string end character or entity
  18. * @return string
  19. */
  20. public static function limit_words($str, $limit = 100, $end_char = nil) {
  21. $limit = (int) $limit;
  22. $end_char = ($end_char === nil) ? '&#8230;' : $end_char;
  23. if(trim($str) === '')
  24. return $str;
  25. if($limit <= 0)
  26. return $end_char;
  27. preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/u', $str, $matches);
  28. // Only attach the end character if the matched string is shorter
  29. // than the starting string.
  30. return rtrim($matches[0]).(strlen($matches[0]) === strlen($str) ? '' : $end_char);
  31. }
  32. /**
  33. * Limits a phrase to a given number of characters.
  34. *
  35. * @param string phrase to limit characters of
  36. * @param integer number of characters to limit to
  37. * @param string end character or entity
  38. * @param boolean enable or disable the preservation of words while limiting
  39. * @return string
  40. */
  41. public static function limit_chars($str, $limit = 100, $end_char = nil, $preserve_words = NO) {
  42. $end_char = ($end_char === nil) ? '&#8230;' : $end_char;
  43. $limit = (int) $limit;
  44. if(trim($str) === '' OR mb_strlen($str) <= $limit)
  45. return $str;
  46. if($limit <= 0)
  47. return $end_char;
  48. if($preserve_words == NO) {
  49. return rtrim(mb_substr($str, 0, $limit)).$end_char;
  50. }
  51. preg_match('/^.{'.($limit - 1).'}\S*/us', $str, $matches);
  52. return rtrim($matches[0]).(strlen($matches[0]) == strlen($str) ? '' : $end_char);
  53. }
  54. /**
  55. * Alternates between two or more strings.
  56. *
  57. * @param string strings to alternate between
  58. * @return string
  59. */
  60. public static function alternate() {
  61. static $i;
  62. if(func_num_args() === 0) {
  63. $i = 0;
  64. return '';
  65. }
  66. $args = func_get_args();
  67. return $args[($i++ % count($args))];
  68. }
  69. /**
  70. * Generates a random string of a given type and length.
  71. *
  72. * @param string a type of pool, or a string of characters to use as the pool
  73. * @param integer length of string to return
  74. * @return string
  75. *
  76. * @tutorial alnum - alpha-numeric characters
  77. * @tutorial alpha - alphabetical characters
  78. * @tutorial numeric - digit characters, 0-9
  79. * @tutorial nozero - digit characters, 1-9
  80. * @tutorial distinct - clearly distinct alpha-numeric characters
  81. */
  82. public static function random($type = 'alnum', $length = 8) {
  83. $utf8 = NO;
  84. switch ($type) {
  85. case 'alnum':
  86. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  87. break;
  88. case 'alpha':
  89. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  90. break;
  91. case 'numeric':
  92. $pool = '0123456789';
  93. break;
  94. case 'nozero':
  95. $pool = '123456789';
  96. break;
  97. case 'distinct':
  98. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  99. break;
  100. default:
  101. $pool = (string) $type;
  102. $utf8 =!str::is_ascii($pool);
  103. break;
  104. }
  105. $str = '';
  106. $pool_size = ($utf8 === YES) ? mb_strlen($pool) : strlen($pool);
  107. for($i = 0; $i < $length; $i++) {
  108. $str .= ($utf8 === YES)
  109. ? mb_substr($pool, mt_rand(0, $pool_size - 1), 1)
  110. : substr($pool, mt_rand(0, $pool_size - 1), 1);
  111. }
  112. return $str;
  113. }
  114. /**
  115. * Reduces multiple slashes in a string to single slashes.
  116. *
  117. * @param string string to reduce slashes of
  118. * @return string
  119. */
  120. public static function reduce_slashes($str) {
  121. return preg_replace('#(?<!:)//+#', '/', $str);
  122. }
  123. /**
  124. * Replaces the given words with a string.
  125. *
  126. * @param string phrase to replace words in
  127. * @param array words to replace
  128. * @param string replacement string
  129. * @param boolean replace words across word boundries (space, period, etc)
  130. * @return string
  131. */
  132. public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = NO) {
  133. foreach((array) $badwords as $key => $badword) {
  134. $badwords[$key] = str_replace('\*', '\S*?', preg_quote((string) $badword));
  135. }
  136. $regex = '('.implode('|', $badwords).')';
  137. if($replace_partial_words == YES) {
  138. // Just using \b isn't sufficient when we need to replace a badword that already contains word boundaries itself
  139. $regex = '(?<=\b|\s|^)'.$regex.'(?=\b|\s|$)';
  140. }
  141. $regex = '!'.$regex.'!ui';
  142. if(mb_strlen($replacement) == 1) {
  143. $regex .= 'e';
  144. return preg_replace($regex, 'str_repeat($replacement, mb_strlen(\'$1\')', $str);
  145. }
  146. return preg_replace($regex, $replacement, $str);
  147. }
  148. /**
  149. * Finds the text that is similar between a set of words.
  150. *
  151. * @param array words to find similar text of
  152. * @return string
  153. */
  154. public static function similar(array $words) {
  155. // First word is the word to match against
  156. $word = current($words);
  157. for($i = 0, $max = strlen($word); $i < $max; ++$i) {
  158. foreach($words as $w) {
  159. // Once a difference is found, break out of the loops
  160. if(!isset($w[$i]) OR $w[$i] !== $word[$i])
  161. break 2;
  162. }
  163. }
  164. // Return the similar text
  165. return substr($word, 0, $i);
  166. }
  167. /**
  168. * Converts text email addresses and anchors into links.
  169. *
  170. * @param string text to auto link
  171. * @return string
  172. */
  173. public static function auto_link($text) {
  174. // Auto link emails first to prevent problems with "www.domain.com@example.com"
  175. return str::auto_link_urls(str::auto_link_emails($text));
  176. }
  177. /**
  178. * Converts text anchors into links.
  179. *
  180. * @param string text to auto link
  181. * @return string
  182. */
  183. public static function auto_link_urls($text) {
  184. // Finds all http/https/ftp/ftps links that are not part of an existing html anchor
  185. if(preg_match_all('~\b(?<!href="|">)(?:ht|f)tps?://\S+(?:/|\b)~i', $text, $matches)) {
  186. foreach($matches[0] as $match) {
  187. // Replace each link with an anchor
  188. $text = str_replace($match, html::anchor($match), $text);
  189. }
  190. }
  191. // Find all naked www.links.com (without http://)
  192. if(preg_match_all('~\b(?<!://)www(?:\.[a-z0-9][-a-z0-9]*+)+\.[a-z]{2,6}\b~i', $text, $matches)) {
  193. foreach($matches[0] as $match) {
  194. // Replace each link with an anchor
  195. $text = str_replace($match, html::anchor('http://'.$match, $match), $text);
  196. }
  197. }
  198. return $text;
  199. }
  200. /**
  201. * Converts text email addresses into links.
  202. *
  203. * @param string text to auto link
  204. * @return string
  205. */
  206. public static function auto_link_emails($text) {
  207. // Finds all email addresses that are not part of an existing html mailto anchor
  208. // Note: The "58;" negative lookbehind prevents matching of existing encoded html mailto anchors
  209. // The html entity for a colon (:) is &#58; or &#058; or &#0058; etc.
  210. if(preg_match_all('~\b(?<!href="mailto:|">|58;)(?!\.)[-+_a-z0-9.]++(?<!\.)@(?![-.])[-a-z0-9.]+(?<!\.)\.[a-z]{2,6}\b~i', $text, $matches)) {
  211. foreach($matches[0] as $match) {
  212. // Replace each email with an encoded mailto
  213. $text = str_replace($match, html::mailto($match), $text);
  214. }
  215. }
  216. return $text;
  217. }
  218. /**
  219. * Automatically applies <p> and <br /> markup to text. Basically nl2br() on steroids.
  220. *
  221. * @param string subject
  222. * @return string
  223. */
  224. public static function auto_p($str) {
  225. // Trim whitespace
  226. if(($str = trim($str)) === '')
  227. return '';
  228. // Standardize newlines
  229. $str = str_replace(array("\r\n", "\r"), "\n", $str);
  230. // Trim whitespace on each line
  231. $str = preg_replace('~^[ \t]+~m', '', $str);
  232. $str = preg_replace('~[ \t]+$~m', '', $str);
  233. // The following regexes only need to be executed if the string contains html
  234. if($html_found = (strpos($str, '<') !== NO)) {
  235. // Elements that should not be surrounded by p tags
  236. $no_p = '(?:p|div|h[1-6r]|ul|ol|li|blockquote|d[dlt]|pre|t[dhr]|t(?:able|body|foot|head)|c(?:aption|olgroup)|form|s(?:elect|tyle)|a(?:ddress|rea)|ma(?:p|th))';
  237. // Put at least two linebreaks before and after $no_p elements
  238. $str = preg_replace('~^<'.$no_p.'[^>]*+>~im', "\n$0", $str);
  239. $str = preg_replace('~</'.$no_p.'\s*+>$~im', "$0\n", $str);
  240. }
  241. // Do the <p> magic!
  242. $str = '<p>'.trim($str).'</p>';
  243. $str = preg_replace('~\n{2,}~', "</p>\n\n<p>", $str);
  244. // The following regexes only need to be executed if the string contains html
  245. if($html_found !== NO) {
  246. // Remove p tags around $no_p elements
  247. $str = preg_replace('~<p>(?=</?'.$no_p.'[^>]*+>)~i', '', $str);
  248. $str = preg_replace('~(</?'.$no_p.'[^>]*+>)</p>~i', '$1', $str);
  249. }
  250. // Convert single linebreaks to <br />
  251. $str = preg_replace('~(?<!\n)\n(?!\n)~', "<br />\n", $str);
  252. return $str;
  253. }
  254. /**
  255. * Returns human readable sizes.
  256. * @see Based on original functions written by:
  257. * @see Aidan Lister: http://aidanlister.com/repos/v/function.size_readable.php
  258. * @see Quentin Zervaas: http://www.phpriot.com/d/code/strings/filesize-format/
  259. *
  260. * @param integer size in bytes
  261. * @param string a definitive unit
  262. * @param string the return string format
  263. * @param boolean whether to use SI prefixes or IEC
  264. * @return string
  265. */
  266. public static function bytes($bytes, $force_unit = nil, $format = nil, $si = YES) {
  267. // Format string
  268. $format = ($format === nil) ? '%01.2f %s' : (string) $format;
  269. // IEC prefixes (binary)
  270. if($si == NO OR strpos($force_unit, 'i') !== NO) {
  271. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  272. $mod = 1024;
  273. }
  274. // SI prefixes (decimal)
  275. else {
  276. $units = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
  277. $mod = 1000;
  278. }
  279. // Determine unit to use
  280. if(($power = array_search((string) $force_unit, $units)) === NO) {
  281. $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
  282. }
  283. return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
  284. }
  285. /**
  286. * Prevents widow words by inserting a non-breaking space between the last two words.
  287. * @see http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin
  288. *
  289. * @param string string to remove widows from
  290. * @return string
  291. */
  292. public static function widont($str) {
  293. $str = rtrim($str);
  294. $space = strrpos($str, ' ');
  295. if($space !== NO) {
  296. $str = substr($str, 0, $space).'&nbsp;'.substr($str, $space + 1);
  297. }
  298. return $str;
  299. }
  300. /**
  301. * Allows you to use the "empty" feature of PHP in a more flexible manner
  302. * @see http://php.net/manual/en/function.empty.php
  303. *
  304. * @param string string to check if empty
  305. * @return string
  306. */
  307. public static function e($var) {
  308. $v = $var;
  309. return empty($v);
  310. }
  311. /**
  312. * Check if a string starts with either a string or an array of strings
  313. *
  314. * @param string haystack, string to check against
  315. * @param mixed needle(s), a single string or an array of strings
  316. * @param bool case sensitive, default is false
  317. * @return string
  318. */
  319. public static function starts_with($str, $arr, $case=false) {
  320. if(!is_array($arr)) {
  321. $arr = array($arr);
  322. }
  323. $str = $case ? $str : strtolower($str);
  324. $slen = strlen($str);
  325. foreach($arr as $cmp) {
  326. $cmp = $case ? $cmp : strtolower($cmp);
  327. $clen = strlen($cmp);
  328. if($clen > $slen) continue;
  329. if(substr($str, 0, $clen) == $cmp) return true;
  330. }
  331. return false;
  332. }
  333. /**
  334. * Check if a string end with either a string or an array of strings
  335. *
  336. * @param string haystack, string to check against
  337. * @param mixed needle(s), a single string or an array of strings
  338. * @param bool case sensitive, default is false
  339. * @return string
  340. */
  341. public static function ends_with($str, $arr, $case=false) {
  342. if(!is_array($arr))
  343. $arr = array($arr);
  344. $str = $case ? $str : strtolower($str);
  345. $slen = strlen($str);
  346. foreach($arr as $cmp) {
  347. $cmp = $case ? $cmp : strtolower($cmp);
  348. $clen = strlen($cmp);
  349. if($clen > $slen) continue;
  350. if(substr($str, $clen*-1) == $cmp) return true;
  351. }
  352. return false;
  353. }
  354. /**
  355. * Check if a string contains either a string or an array of strings
  356. *
  357. * @param string haystack, string to check against
  358. * @param mixed needle(s), a single string or an array of strings
  359. * @param bool case sensitive, default is false
  360. * @return string
  361. */
  362. public static function contains($str, $arr, $case=false) {
  363. !is_array($arr) && $arr = array($arr);
  364. $func = $case ? "strstr" : "stristr";
  365. foreach($arr as $cmp)
  366. if($func($str, $cmp)) return true;
  367. return false;
  368. }
  369. /**
  370. * Check if a string is a path, a valid URL is condsidered a path.
  371. *
  372. * @param string string to check if is path/url
  373. * @return bool
  374. */
  375. public static function is_path($path) {
  376. if(self::is_url($path)) {
  377. return true;
  378. }
  379. if(self::contains($path, array("\n", "\r", "\t"))) {
  380. return false;
  381. }
  382. if(file_exists($path) && is_file($path)) {
  383. return true;
  384. }
  385. return false;
  386. }
  387. /**
  388. * Check if a string is a URL
  389. *
  390. * @param string string to check if is url
  391. * @return bool
  392. */
  393. public static function is_url($path) {
  394. if(self::starts_with($path, array("http://", "https://", "ftp://", "feed://"))) {
  395. return true;
  396. } else {
  397. return false;
  398. }
  399. }
  400. /**
  401. * Get an element in a delimeter separated string
  402. *
  403. * @param string delimeter
  404. * @param string string with elements separated by delimeter
  405. * @param integer index of element to get
  406. * @return bool
  407. */
  408. public static function get_at($delimeter, $haystack, $get=0) {
  409. $var = explode($delimeter,$haystack);
  410. return $var[$get];
  411. }
  412. /**
  413. * Get last element in a delimeter separated string
  414. *
  415. * @param string delimeter
  416. * @param string string with elements separated by delimeter
  417. * @return bool
  418. */
  419. public static function get_last($delimeter, $haystack) {
  420. $var = explode($delimeter,$haystack);
  421. return end($var);
  422. }
  423. public static function contains_only(&$str, $pattern, $trim=false) {
  424. $trim && $str = trim($str);
  425. return strlen(preg_replace("/[".$pattern."]/", "", $str)) == 0;
  426. }
  427. /**
  428. * Strip everything in a string except what matches the regex pattern
  429. *
  430. * @param string string to strip
  431. * @param string regex string to preserve
  432. * @return string
  433. */
  434. public static function strip_all_but($str, $pattern) {
  435. return preg_replace("/([^".$pattern."]*)/", "", $str);
  436. }
  437. /**
  438. * Turn a string into "yes" or "no"
  439. * If a string equals 1 or string, or starts with "y/Y" or "t/T" it will return "yes", otherwise it will return "no"
  440. *
  441. * @param string
  442. * @return string "yes" or "no"
  443. */
  444. public static function yesno($str) {
  445. if($str == 1 OR $str == true OR (strlen($str) > 0 AND (strtolower($str{0}) == 'y' OR strtolower($str{0}) == 't'))) {
  446. return "yes";
  447. } else {
  448. return "no";
  449. }
  450. }
  451. /**
  452. * Toggle wildcards in a string between asterisks and SQL compatible percentage signs
  453. *
  454. * @param string
  455. * @return string
  456. */
  457. public static function wildcard($str, $back=false) {
  458. if($back) return str_replace("%", "*", $str);
  459. else return str_replace("*", "%", $str);
  460. }
  461. public static function between($str, $start, $end) {
  462. $str = explode($start, $str);
  463. $str = $str[1];
  464. $str = explode($end, $str);
  465. return $str[0];
  466. }
  467. /**
  468. * Checks if $count and returns either a plural or singular string of the given string
  469. *
  470. * @param string initial word, should be singular
  471. * @param integer item count
  472. * @return string singular or plural version of initial word
  473. */
  474. public static function plural($var, $count) {
  475. if($count == 1) return inflector::singular($var);
  476. else return inflector::plural($var);
  477. }
  478. /**
  479. * Replaces BR tags in a string with newlines.
  480. *
  481. * @param string string containing br tags
  482. * @return string
  483. */
  484. public static function br2nl($var) {
  485. return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
  486. }
  487. /**
  488. * Better version of PHP's gzuncompress
  489. * @see http://www.php.net/manual/en/function.gzuncompress.php
  490. *
  491. * @param data gzipped data
  492. * @return string
  493. */
  494. public static function gzuncompress($data) {
  495. $f = tempnam('/tmp', 'gz_fix');
  496. file_put_contents($f, $data);
  497. return file_get_contents('compress.zlib://'.$f);
  498. }
  499. /**
  500. * Hard wraps a string after a number of characters pass
  501. *
  502. * @param string long string
  503. * @param integer length of each string
  504. * @return string
  505. */
  506. public static function hard_wrap($str, $to_width=100) {
  507. $lines = array();
  508. while(strlen($str) > $to_width) {
  509. $lines[] = substr($str, 0, $to_width);
  510. $str = substr($str, $to_width);
  511. }
  512. $lines[] = $str;
  513. return implode($lines, "\n");
  514. }
  515. /**
  516. * Tests whether a string contains only 7bit ASCII bytes. This is used to
  517. * determine when to use native functions or UTF-8 functions.
  518. *
  519. * ##### Example
  520. *
  521. * $str = 'abcd';
  522. * var_export(str::is_ascii($str));
  523. *
  524. * // Output:
  525. * true
  526. *
  527. * @see http://sourceforge.net/projects/phputf8/
  528. * @copyright (c) 2005 Harry Fuecks
  529. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  530. *
  531. * @param string $str String to check
  532. * @return bool
  533. */
  534. public static function is_ascii($str) {
  535. return is_string($str) AND ! preg_match('/[^\x00-\x7F]/S', $str);
  536. }
  537. /**
  538. * Strips out device control codes in the ASCII range.
  539. *
  540. * ##### Example
  541. *
  542. * $result = str::strip_ascii_ctrl($str_containing_control_codes);
  543. *
  544. * @link http://sourceforge.net/projects/phputf8/
  545. * @copyright (c) 2005 Harry Fuecks
  546. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  547. *
  548. * @param string $str String to clean
  549. * @return string
  550. */
  551. public static function strip_ascii_ctrl($str) {
  552. return preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S', '', $str);
  553. }
  554. /**
  555. * Strips out all non-7bit ASCII bytes.
  556. *
  557. * For a description of the difference between 7bit and
  558. * extended (8bit) ASCII:
  559. *
  560. * @link http://en.wikipedia.org/wiki/ASCII
  561. *
  562. * ##### Example
  563. *
  564. * $result = str::strip_non_ascii($str_with_8bit_ascii);
  565. *
  566. * @link http://sourceforge.net/projects/phputf8/
  567. * @copyright (c) 2005 Harry Fuecks
  568. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  569. *
  570. * @param string $str String to clean
  571. * @return string
  572. */
  573. public static function strip_non_ascii($str) {
  574. return preg_replace('/[^\x00-\x7F]+/S', '', $str);
  575. }
  576. /**
  577. * Replaces special/accented UTF-8 characters by ASCII-7 'equivalents'.
  578. *
  579. * ##### Example
  580. *
  581. * echo str::transliterate_to_ascii("Útgarðar");
  582. *
  583. * // Output:
  584. * Utgardhar
  585. *
  586. * @author Andreas Gohr <andi@splitbrain.org>
  587. * @link http://sourceforge.net/projects/phputf8/
  588. * @copyright (c) 2005 Harry Fuecks
  589. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  590. *
  591. * @param string $str String to transliterate
  592. * @param integer $case -1 lowercase only, +1 uppercase only, 0 both cases
  593. * @return string
  594. */
  595. public static function transliterate_to_ascii($str, $case = 0) {
  596. static $UTF8_LOWER_ACCENTS = NULL;
  597. static $UTF8_UPPER_ACCENTS = NULL;
  598. if ($case <= 0) {
  599. if ($UTF8_LOWER_ACCENTS === NULL) {
  600. $UTF8_LOWER_ACCENTS = array(
  601. 'à' => 'a', 'ô' => 'o', 'ď' => 'd', 'ḟ' => 'f', 'ë' => 'e', 'š' => 's', 'ơ' => 'o',
  602. 'ß' => 'ss', 'ă' => 'a', 'ř' => 'r', 'ț' => 't', 'ň' => 'n', 'ā' => 'a', 'ķ' => 'k',
  603. 'ŝ' => 's', 'ỳ' => 'y', 'ņ' => 'n', 'ĺ' => 'l', 'ħ' => 'h', 'ṗ' => 'p', 'ó' => 'o',
  604. 'ú' => 'u', 'ě' => 'e', 'é' => 'e', 'ç' => 'c', 'ẁ' => 'w', 'ċ' => 'c', 'õ' => 'o',
  605. 'ṡ' => 's', 'ø' => 'o', 'ģ' => 'g', 'ŧ' => 't', 'ș' => 's', 'ė' => 'e', 'ĉ' => 'c',
  606. 'ś' => 's', 'î' => 'i', 'ű' => 'u', 'ć' => 'c', 'ę' => 'e', 'ŵ' => 'w', 'ṫ' => 't',
  607. 'ū' => 'u', 'č' => 'c', 'ö' => 'o', 'è' => 'e', 'ŷ' => 'y', 'ą' => 'a', 'ł' => 'l',
  608. 'ų' => 'u', 'ů' => 'u', 'ş' => 's', 'ğ' => 'g', 'ļ' => 'l', 'ƒ' => 'f', 'ž' => 'z',
  609. 'ẃ' => 'w', 'ḃ' => 'b', 'å' => 'a', 'ì' => 'i', 'ï' => 'i', 'ḋ' => 'd', 'ť' => 't',
  610. 'ŗ' => 'r', 'ä' => 'a', 'í' => 'i', 'ŕ' => 'r', 'ê' => 'e', 'ü' => 'u', 'ò' => 'o',
  611. 'ē' => 'e', 'ñ' => 'n', 'ń' => 'n', 'ĥ' => 'h', 'ĝ' => 'g', 'đ' => 'd', 'ĵ' => 'j',
  612. 'ÿ' => 'y', 'ũ' => 'u', 'ŭ' => 'u', 'ư' => 'u', 'ţ' => 't', 'ý' => 'y', 'ő' => 'o',
  613. 'â' => 'a', 'ľ' => 'l', 'ẅ' => 'w', 'ż' => 'z', 'ī' => 'i', 'ã' => 'a', 'ġ' => 'g',
  614. 'ṁ' => 'm', 'ō' => 'o', 'ĩ' => 'i', 'ù' => 'u', 'į' => 'i', 'ź' => 'z', 'á' => 'a',
  615. 'û' => 'u', 'þ' => 'th', 'ð' => 'dh', 'æ' => 'ae', 'µ' => 'u', 'ĕ' => 'e', 'ı' => 'i',
  616. );
  617. }
  618. $str = str_replace(
  619. array_keys($UTF8_LOWER_ACCENTS),
  620. array_values($UTF8_LOWER_ACCENTS),
  621. $str
  622. );
  623. }
  624. if ($case >= 0) {
  625. if ($UTF8_UPPER_ACCENTS === NULL) {
  626. $UTF8_UPPER_ACCENTS = array(
  627. 'À' => 'A', 'Ô' => 'O', 'Ď' => 'D', 'Ḟ' => 'F', 'Ë' => 'E', 'Š' => 'S', 'Ơ' => 'O',
  628. 'Ă' => 'A', 'Ř' => 'R', 'Ț' => 'T', 'Ň' => 'N', 'Ā' => 'A', 'Ķ' => 'K', 'Ĕ' => 'E',
  629. 'Ŝ' => 'S', 'Ỳ' => 'Y', 'Ņ' => 'N', 'Ĺ' => 'L', 'Ħ' => 'H', 'Ṗ' => 'P', 'Ó' => 'O',
  630. 'Ú' => 'U', 'Ě' => 'E', 'É' => 'E', 'Ç' => 'C', 'Ẁ' => 'W', 'Ċ' => 'C', 'Õ' => 'O',
  631. 'Ṡ' => 'S', 'Ø' => 'O', 'Ģ' => 'G', 'Ŧ' => 'T', 'Ș' => 'S', 'Ė' => 'E', 'Ĉ' => 'C',
  632. 'Ś' => 'S', 'Î' => 'I', 'Ű' => 'U', 'Ć' => 'C', 'Ę' => 'E', 'Ŵ' => 'W', 'Ṫ' => 'T',
  633. 'Ū' => 'U', 'Č' => 'C', 'Ö' => 'O', 'È' => 'E', 'Ŷ' => 'Y', 'Ą' => 'A', 'Ł' => 'L',
  634. 'Ų' => 'U', 'Ů' => 'U', 'Ş' => 'S', 'Ğ' => 'G', 'Ļ' => 'L', 'Ƒ' => 'F', 'Ž' => 'Z',
  635. 'Ẃ' => 'W', 'Ḃ' => 'B', 'Å' => 'A', 'Ì' => 'I', 'Ï' => 'I', 'Ḋ' => 'D', 'Ť' => 'T',
  636. 'Ŗ' => 'R', 'Ä' => 'A', 'Í' => 'I', 'Ŕ' => 'R', 'Ê' => 'E', 'Ü' => 'U', 'Ò' => 'O',
  637. 'Ē' => 'E', 'Ñ' => 'N', 'Ń' => 'N', 'Ĥ' => 'H', 'Ĝ' => 'G', 'Đ' => 'D', 'Ĵ' => 'J',
  638. 'Ÿ' => 'Y', 'Ũ' => 'U', 'Ŭ' => 'U', 'Ư' => 'U', 'Ţ' => 'T', 'Ý' => 'Y', 'Ő' => 'O',
  639. 'Â' => 'A', 'Ľ' => 'L', 'Ẅ' => 'W', 'Ż' => 'Z', 'Ī' => 'I', 'Ã' => 'A', 'Ġ' => 'G',
  640. 'Ṁ' => 'M', 'Ō' => 'O', 'Ĩ' => 'I', 'Ù' => 'U', 'Į' => 'I', 'Ź' => 'Z', 'Á' => 'A',
  641. 'Û' => 'U', 'Þ' => 'Th', 'Ð' => 'Dh', 'Æ' => 'Ae', 'İ' => 'I',
  642. );
  643. }
  644. $str = str_replace(
  645. array_keys($UTF8_UPPER_ACCENTS),
  646. array_values($UTF8_UPPER_ACCENTS),
  647. $str
  648. );
  649. }
  650. return $str;
  651. }
  652. } // End str