PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Gedmo/Sluggable/Util/Urlizer.php

https://github.com/jaimesuez/DoctrineExtensions
PHP | 461 lines | 298 code | 29 blank | 134 comment | 67 complexity | ed38ba3c3a98f6b420c5061abd12a3d5 MD5 | raw file
  1. <?php
  2. namespace Gedmo\Sluggable\Util;
  3. /**
  4. * This is the part taken from Doctrine 1.2.3
  5. * Doctrine inflector has static methods for inflecting text
  6. *
  7. * The methods in these classes are from several different sources collected
  8. * across several different php projects and several different authors. The
  9. * original author names and emails are not known
  10. *
  11. * Uses 3rd party libraries and functions:
  12. * http://sourceforge.net/projects/phputf8
  13. *
  14. * @package Gedmo.Sluggable.Util
  15. * @subpackage Urlizer
  16. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  17. * @link www.doctrine-project.org
  18. * @since 1.0
  19. * @version $Revision: 3189 $
  20. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  21. * @author Jonathan H. Wage <jonwage@gmail.com>
  22. * @author <hsivonen@iki.fi>
  23. */
  24. class Urlizer
  25. {
  26. /**
  27. * Check if a string has utf7 characters in it
  28. *
  29. * By bmorel at ssi dot fr
  30. *
  31. * @param string $string
  32. * @return boolean $bool
  33. */
  34. public static function seemsUtf8($string)
  35. {
  36. for ($i = 0; $i < strlen($string); $i++) {
  37. if (ord($string[$i]) < 0x80) continue; # 0bbbbbbb
  38. elseif ((ord($string[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
  39. elseif ((ord($string[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
  40. elseif ((ord($string[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
  41. elseif ((ord($string[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
  42. elseif ((ord($string[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
  43. else return false; # Does not match any model
  44. for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
  45. if ((++$i == strlen($string)) || ((ord($string[$i]) & 0xC0) != 0x80))
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. /**
  52. * Remove any illegal characters, accents, etc.
  53. *
  54. * @param string $string String to unaccent
  55. * @return string $string Unaccented string
  56. */
  57. public static function unaccent($string)
  58. {
  59. if (!preg_match('/[\x80-\xff]/', $string)) {
  60. return $string;
  61. }
  62. if (self::seemsUtf8($string)) {
  63. $chars = array(
  64. // Decompositions for Latin-1 Supplement
  65. chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
  66. chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
  67. chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
  68. chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
  69. chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
  70. chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
  71. chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
  72. chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
  73. chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
  74. chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
  75. chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
  76. chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
  77. chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
  78. chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
  79. chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
  80. chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
  81. chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
  82. chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
  83. chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
  84. chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
  85. chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
  86. chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
  87. chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
  88. chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
  89. chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
  90. chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
  91. chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
  92. chr(195).chr(191) => 'y',
  93. // Decompositions for Latin Extended-A
  94. chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
  95. chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
  96. chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
  97. chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
  98. chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
  99. chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
  100. chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
  101. chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
  102. chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
  103. chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
  104. chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
  105. chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
  106. chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
  107. chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
  108. chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
  109. chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
  110. chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
  111. chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
  112. chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
  113. chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
  114. chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
  115. chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
  116. chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
  117. chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
  118. chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
  119. chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
  120. chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
  121. chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
  122. chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
  123. chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
  124. chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
  125. chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
  126. chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
  127. chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
  128. chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
  129. chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
  130. chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
  131. chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
  132. chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
  133. chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
  134. chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
  135. chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
  136. chr(197).chr(148) => 'R', chr(197).chr(149) => 'r',
  137. chr(197).chr(150) => 'R', chr(197).chr(151) => 'r',
  138. chr(197).chr(152) => 'R', chr(197).chr(153) => 'r',
  139. chr(197).chr(154) => 'S', chr(197).chr(155) => 's',
  140. chr(197).chr(156) => 'S', chr(197).chr(157) => 's',
  141. chr(197).chr(158) => 'S', chr(197).chr(159) => 's',
  142. chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
  143. chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
  144. chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
  145. chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
  146. chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
  147. chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
  148. chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
  149. chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
  150. chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
  151. chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
  152. chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
  153. chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
  154. chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
  155. chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
  156. chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
  157. chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
  158. // Euro Sign
  159. chr(226).chr(130).chr(172) => 'E',
  160. // GBP (Pound) Sign
  161. chr(194).chr(163) => '',
  162. 'Ä' => 'Ae', 'ä' => 'ae', 'Ü' => 'Ue', 'ü' => 'ue',
  163. 'Ö' => 'Oe', 'ö' => 'oe', 'ß' => 'ss',
  164. // Norwegian characters
  165. 'Å'=>'Aa','Æ'=>'Ae','Ø'=>'O','æ'=>'a','ø'=>'o','å'=>'aa'
  166. );
  167. $string = strtr($string, $chars);
  168. } else {
  169. // Assume ISO-8859-1 if not UTF-8
  170. $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
  171. .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
  172. .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
  173. .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
  174. .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
  175. .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
  176. .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
  177. .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
  178. .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
  179. .chr(252).chr(253).chr(255);
  180. $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
  181. $string = strtr($string, $chars['in'], $chars['out']);
  182. $doubleChars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
  183. $doubleChars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
  184. $string = str_replace($doubleChars['in'], $doubleChars['out'], $string);
  185. }
  186. return $string;
  187. }
  188. /**
  189. * US-ASCII transliterations of Unicode text
  190. * Ported Sean M. Burke's Text::Unidecode Perl module (He did all the hard work!)
  191. * Warning: you should only pass this well formed UTF-8!
  192. * Be aware it works by making a copy of the input string which it appends transliterated
  193. * characters to - it uses a PHP output buffer to do this - it means, memory use will increase,
  194. * requiring up to the same amount again as the input string
  195. *
  196. * @see http://search.cpan.org/~sburke/Text-Unidecode-0.04/lib/Text/Unidecode.pm
  197. * @param string UTF-8 string to convert
  198. * @author <hsivonen@iki.fi>
  199. * @param string (default = ?) Character use if character unknown
  200. * @return string US-ASCII string
  201. */
  202. public static function utf8ToAscii($str, $unknown = '?') {
  203. # The database for transliteration stored here
  204. static $UTF8_TO_ASCII = array();
  205. # Variable lookups faster than accessing constants
  206. $UTF8_TO_ASCII_DB = __DIR__ . '/data';
  207. if (strlen($str) == 0) {
  208. return '';
  209. }
  210. $len = strlen($str);
  211. $i = 0;
  212. $result = '';
  213. while ($i < $len) {
  214. $ord = NULL;
  215. $increment = 1;
  216. $ord0 = ord($str{$i});
  217. # Much nested if /else - PHP fn calls expensive, no block scope...
  218. # 1 byte - ASCII
  219. if ($ord0 >= 0 && $ord0 <= 127) {
  220. $ord = $ord0;
  221. $increment = 1;
  222. } else {
  223. # 2 bytes
  224. $ord1 = ord($str{$i+1});
  225. if ($ord0 >= 192 && $ord0 <= 223) {
  226. $ord = ($ord0 - 192) * 64 + ($ord1 - 128);
  227. $increment = 2;
  228. } else {
  229. # 3 bytes
  230. $ord2 = ord($str{$i+2});
  231. if ($ord0 >= 224 && $ord0 <= 239) {
  232. $ord = ($ord0-224)*4096 + ($ord1-128)*64 + ($ord2-128);
  233. $increment = 3;
  234. } else {
  235. # 4 bytes
  236. $ord3 = ord($str{$i+3});
  237. if ($ord0>=240 && $ord0<=247) {
  238. $ord = ($ord0-240)*262144 + ($ord1-128)*4096
  239. + ($ord2-128)*64 + ($ord3-128);
  240. $increment = 4;
  241. } else {
  242. throw new \Gedmo\Exception\UnexpectedValueException('Unidentified ut8 character was present, pure utf8 required');
  243. }
  244. }
  245. }
  246. }
  247. $bank = $ord >> 8;
  248. # If we haven't used anything from this bank before, need to load it...
  249. if (!array_key_exists($bank, $UTF8_TO_ASCII)) {
  250. $bankfile = $UTF8_TO_ASCII_DB. '/'. sprintf("x%02x", $bank).'.php';
  251. if (file_exists($bankfile)) {
  252. # Load the appropriate database
  253. if (!include $bankfile) {
  254. throw new \Gedmo\Exception\RuntimeException('Cannot find character bank file: ' . $bankfile);
  255. }
  256. } else {
  257. # Some banks are deliberately empty
  258. $UTF8_TO_ASCII[$bank] = array();
  259. }
  260. }
  261. $newchar = $ord & 255;
  262. if (isset($UTF8_TO_ASCII[$bank]) && array_key_exists($newchar, $UTF8_TO_ASCII[$bank])) {
  263. $result .= $UTF8_TO_ASCII[$bank][$newchar];
  264. } else {
  265. $result .= $unknown;
  266. }
  267. $i += $increment;
  268. }
  269. return $result;
  270. }
  271. /**
  272. * Does not transliterate correctly eastern languages
  273. *
  274. * @param string $text
  275. * @param string $separator
  276. * @return string
  277. */
  278. public static function urlize($text, $separator = '-')
  279. {
  280. $text = self::unaccent($text);
  281. return self::postProcessText($text, $separator);
  282. }
  283. /**
  284. * Uses transliteration tables to convert any kind of utf8 character
  285. *
  286. * @param string $text
  287. * @param string $separator
  288. * @return string $text
  289. */
  290. public static function transliterate($text, $separator = '-')
  291. {
  292. if (preg_match('/[\x80-\xff]/', $text) && self::validUtf8($text)) {
  293. $text = self::utf8ToAscii($text);
  294. }
  295. return self::postProcessText($text, $separator);
  296. }
  297. /**
  298. * Tests a string as to whether it's valid UTF-8 and supported by the
  299. * Unicode standard
  300. * Note: this function has been modified to simple return true or false
  301. * @author <hsivonen@iki.fi>
  302. * @param string UTF-8 encoded string
  303. * @return boolean true if valid
  304. * @see http://hsivonen.iki.fi/php-utf8/
  305. */
  306. public static function validUtf8($str)
  307. {
  308. $mState = 0; // cached expected number of octets after the current octet
  309. // until the beginning of the next UTF8 character sequence
  310. $mUcs4 = 0; // cached Unicode character
  311. $mBytes = 1; // cached expected number of octets in the current sequence
  312. $len = strlen($str);
  313. for ($i = 0; $i < $len; $i++) {
  314. $in = ord($str{$i});
  315. if ($mState == 0) {
  316. // When mState is zero we expect either a US-ASCII character or a
  317. // multi-octet sequence.
  318. if (0 == (0x80 & ($in))) {
  319. // US-ASCII, pass straight through.
  320. $mBytes = 1;
  321. } elseif (0xC0 == (0xE0 & ($in))) {
  322. // First octet of 2 octet sequence
  323. $mUcs4 = ($in);
  324. $mUcs4 = ($mUcs4 & 0x1F) << 6;
  325. $mState = 1;
  326. $mBytes = 2;
  327. } elseif (0xE0 == (0xF0 & ($in))) {
  328. // First octet of 3 octet sequence
  329. $mUcs4 = ($in);
  330. $mUcs4 = ($mUcs4 & 0x0F) << 12;
  331. $mState = 2;
  332. $mBytes = 3;
  333. } elseif (0xF0 == (0xF8 & ($in))) {
  334. // First octet of 4 octet sequence
  335. $mUcs4 = ($in);
  336. $mUcs4 = ($mUcs4 & 0x07) << 18;
  337. $mState = 3;
  338. $mBytes = 4;
  339. } elseif (0xF8 == (0xFC & ($in))) {
  340. /* First octet of 5 octet sequence.
  341. *
  342. * This is illegal because the encoded codepoint must be either
  343. * (a) not the shortest form or
  344. * (b) outside the Unicode range of 0-0x10FFFF.
  345. * Rather than trying to resynchronize, we will carry on until the end
  346. * of the sequence and let the later error handling code catch it.
  347. */
  348. $mUcs4 = ($in);
  349. $mUcs4 = ($mUcs4 & 0x03) << 24;
  350. $mState = 4;
  351. $mBytes = 5;
  352. } elseif (0xFC == (0xFE & ($in))) {
  353. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  354. $mUcs4 = ($in);
  355. $mUcs4 = ($mUcs4 & 1) << 30;
  356. $mState = 5;
  357. $mBytes = 6;
  358. } else {
  359. /* Current octet is neither in the US-ASCII range nor a legal first
  360. * octet of a multi-octet sequence.
  361. */
  362. return false;
  363. }
  364. } else {
  365. // When mState is non-zero, we expect a continuation of the multi-octet
  366. // sequence
  367. if (0x80 == (0xC0 & ($in))) {
  368. // Legal continuation.
  369. $shift = ($mState - 1) * 6;
  370. $tmp = $in;
  371. $tmp = ($tmp & 0x0000003F) << $shift;
  372. $mUcs4 |= $tmp;
  373. /**
  374. * End of the multi-octet sequence. mUcs4 now contains the final
  375. * Unicode codepoint to be output
  376. */
  377. if (0 == --$mState) {
  378. /*
  379. * Check for illegal sequences and codepoints.
  380. */
  381. // From Unicode 3.1, non-shortest form is illegal
  382. if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
  383. ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
  384. ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
  385. (4 < $mBytes) ||
  386. // From Unicode 3.2, surrogate characters are illegal
  387. (($mUcs4 & 0xFFFFF800) == 0xD800) ||
  388. // Codepoints outside the Unicode range are illegal
  389. ($mUcs4 > 0x10FFFF)
  390. ) {
  391. return false;
  392. }
  393. //initialize UTF8 cache
  394. $mState = 0;
  395. $mUcs4 = 0;
  396. $mBytes = 1;
  397. }
  398. } else {
  399. /**
  400. *((0xC0 & (*in) != 0x80) && (mState != 0))
  401. * Incomplete multi-octet sequence.
  402. */
  403. return false;
  404. }
  405. }
  406. }
  407. return true;
  408. }
  409. /**
  410. * Cleans up the text and adds separator
  411. *
  412. * @param string $text
  413. * @param string $separator
  414. * @return string
  415. */
  416. private static function postProcessText($text, $separator)
  417. {
  418. if (function_exists('mb_strtolower')) {
  419. $text = mb_strtolower($text);
  420. } else {
  421. $text = strtolower($text);
  422. }
  423. // Remove all none word characters
  424. $text = preg_replace('/\W/', ' ', $text);
  425. // More stripping. Replace spaces with dashes
  426. $text = strtolower(preg_replace('/[^A-Z^a-z^0-9^\/]+/', $separator,
  427. preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
  428. preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
  429. preg_replace('/::/', '/', $text)))));
  430. return trim($text, $separator);
  431. }
  432. }