PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Utility/Inflector.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 557 lines | 359 code | 38 blank | 160 comment | 37 complexity | 753ee3790ec7a7a716fbd6ad11cf0cb4 MD5 | raw file
  1. <?php
  2. /**
  3. * Pluralize and singularize English words.
  4. *
  5. * Used by Cake's naming conventions throughout the framework.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Utility
  18. * @since CakePHP(tm) v 0.2.9
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. /**
  22. * Pluralize and singularize English words.
  23. *
  24. * Inflector pluralizes and singularizes English nouns.
  25. * Used by Cake's naming conventions throughout the framework.
  26. *
  27. * @package Cake.Utility
  28. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html
  29. */
  30. class Inflector {
  31. /**
  32. * Plural inflector rules
  33. *
  34. * @var array
  35. */
  36. protected static $_plural = array(
  37. 'rules' => array(
  38. '/(s)tatus$/i' => '\1\2tatuses',
  39. '/(quiz)$/i' => '\1zes',
  40. '/^(ox)$/i' => '\1\2en',
  41. '/([m|l])ouse$/i' => '\1ice',
  42. '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
  43. '/(x|ch|ss|sh)$/i' => '\1es',
  44. '/([^aeiouy]|qu)y$/i' => '\1ies',
  45. '/(hive)$/i' => '\1s',
  46. '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
  47. '/sis$/i' => 'ses',
  48. '/([ti])um$/i' => '\1a',
  49. '/(p)erson$/i' => '\1eople',
  50. '/(m)an$/i' => '\1en',
  51. '/(c)hild$/i' => '\1hildren',
  52. '/(buffal|tomat)o$/i' => '\1\2oes',
  53. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
  54. '/us$/i' => 'uses',
  55. '/(alias)$/i' => '\1es',
  56. '/(ax|cris|test)is$/i' => '\1es',
  57. '/s$/' => 's',
  58. '/^$/' => '',
  59. '/$/' => 's',
  60. ),
  61. 'uninflected' => array(
  62. '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people'
  63. ),
  64. 'irregular' => array(
  65. 'atlas' => 'atlases',
  66. 'beef' => 'beefs',
  67. 'brother' => 'brothers',
  68. 'cafe' => 'cafes',
  69. 'child' => 'children',
  70. 'corpus' => 'corpuses',
  71. 'cow' => 'cows',
  72. 'ganglion' => 'ganglions',
  73. 'genie' => 'genies',
  74. 'genus' => 'genera',
  75. 'graffito' => 'graffiti',
  76. 'hoof' => 'hoofs',
  77. 'loaf' => 'loaves',
  78. 'man' => 'men',
  79. 'money' => 'monies',
  80. 'mongoose' => 'mongooses',
  81. 'move' => 'moves',
  82. 'mythos' => 'mythoi',
  83. 'niche' => 'niches',
  84. 'numen' => 'numina',
  85. 'occiput' => 'occiputs',
  86. 'octopus' => 'octopuses',
  87. 'opus' => 'opuses',
  88. 'ox' => 'oxen',
  89. 'penis' => 'penises',
  90. 'person' => 'people',
  91. 'sex' => 'sexes',
  92. 'soliloquy' => 'soliloquies',
  93. 'testis' => 'testes',
  94. 'trilby' => 'trilbys',
  95. 'turf' => 'turfs'
  96. )
  97. );
  98. /**
  99. * Singular inflector rules
  100. *
  101. * @var array
  102. */
  103. protected static $_singular = array(
  104. 'rules' => array(
  105. '/(s)tatuses$/i' => '\1\2tatus',
  106. '/^(.*)(menu)s$/i' => '\1\2',
  107. '/(quiz)zes$/i' => '\\1',
  108. '/(matr)ices$/i' => '\1ix',
  109. '/(vert|ind)ices$/i' => '\1ex',
  110. '/^(ox)en/i' => '\1',
  111. '/(alias)(es)*$/i' => '\1',
  112. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
  113. '/([ftw]ax)es/i' => '\1',
  114. '/(cris|ax|test)es$/i' => '\1is',
  115. '/(shoe|slave)s$/i' => '\1',
  116. '/(o)es$/i' => '\1',
  117. '/ouses$/' => 'ouse',
  118. '/([^a])uses$/' => '\1us',
  119. '/([m|l])ice$/i' => '\1ouse',
  120. '/(x|ch|ss|sh)es$/i' => '\1',
  121. '/(m)ovies$/i' => '\1\2ovie',
  122. '/(s)eries$/i' => '\1\2eries',
  123. '/([^aeiouy]|qu)ies$/i' => '\1y',
  124. '/([lr])ves$/i' => '\1f',
  125. '/(tive)s$/i' => '\1',
  126. '/(hive)s$/i' => '\1',
  127. '/(drive)s$/i' => '\1',
  128. '/([^fo])ves$/i' => '\1fe',
  129. '/(^analy)ses$/i' => '\1sis',
  130. '/(analy|ba|diagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
  131. '/([ti])a$/i' => '\1um',
  132. '/(p)eople$/i' => '\1\2erson',
  133. '/(m)en$/i' => '\1an',
  134. '/(c)hildren$/i' => '\1\2hild',
  135. '/(n)ews$/i' => '\1\2ews',
  136. '/eaus$/' => 'eau',
  137. '/^(.*us)$/' => '\\1',
  138. '/s$/i' => ''
  139. ),
  140. 'uninflected' => array(
  141. '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
  142. ),
  143. 'irregular' => array(
  144. 'waves' => 'wave',
  145. 'curves' => 'curve'
  146. )
  147. );
  148. /**
  149. * Words that should not be inflected
  150. *
  151. * @var array
  152. */
  153. protected static $_uninflected = array(
  154. 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
  155. 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
  156. 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
  157. 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
  158. 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
  159. 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
  160. 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
  161. 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
  162. 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
  163. 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
  164. 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
  165. 'Yengeese'
  166. );
  167. /**
  168. * Default map of accented and special characters to ASCII characters
  169. *
  170. * @var array
  171. */
  172. protected static $_transliteration = array(
  173. '/ä|æ|ǽ/' => 'ae',
  174. '/ö|œ/' => 'oe',
  175. '/ü/' => 'ue',
  176. '/Ä/' => 'Ae',
  177. '/Ü/' => 'Ue',
  178. '/Ö/' => 'Oe',
  179. '/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
  180. '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a',
  181. '/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
  182. '/ç|ć|ĉ|ċ|č/' => 'c',
  183. '/Ð|Ď|Đ/' => 'D',
  184. '/ð|ď|đ/' => 'd',
  185. '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E',
  186. '/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e',
  187. '/Ĝ|Ğ|Ġ|Ģ/' => 'G',
  188. '/ĝ|ğ|ġ|ģ/' => 'g',
  189. '/Ĥ|Ħ/' => 'H',
  190. '/ĥ|ħ/' => 'h',
  191. '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/' => 'I',
  192. '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/' => 'i',
  193. '/Ĵ/' => 'J',
  194. '/ĵ/' => 'j',
  195. '/Ķ/' => 'K',
  196. '/ķ/' => 'k',
  197. '/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L',
  198. '/ĺ|ļ|ľ|ŀ|ł/' => 'l',
  199. '/Ñ|Ń|Ņ|Ň/' => 'N',
  200. '/ñ|ń|ņ|ň|ʼn/' => 'n',
  201. '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O',
  202. '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o',
  203. '/Ŕ|Ŗ|Ř/' => 'R',
  204. '/ŕ|ŗ|ř/' => 'r',
  205. '/Ś|Ŝ|Ş|Š/' => 'S',
  206. '/ś|ŝ|ş|š|ſ/' => 's',
  207. '/Ţ|Ť|Ŧ/' => 'T',
  208. '/ţ|ť|ŧ/' => 't',
  209. '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U',
  210. '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
  211. '/Ý|Ÿ|Ŷ/' => 'Y',
  212. '/ý|ÿ|ŷ/' => 'y',
  213. '/Ŵ/' => 'W',
  214. '/ŵ/' => 'w',
  215. '/Ź|Ż|Ž/' => 'Z',
  216. '/ź|ż|ž/' => 'z',
  217. '/Æ|Ǽ/' => 'AE',
  218. '/ß/' => 'ss',
  219. '/IJ/' => 'IJ',
  220. '/ij/' => 'ij',
  221. '/Œ/' => 'OE',
  222. '/ƒ/' => 'f'
  223. );
  224. /**
  225. * Method cache array.
  226. *
  227. * @var array
  228. */
  229. protected static $_cache = array();
  230. /**
  231. * The initial state of Inflector so reset() works.
  232. *
  233. * @var array
  234. */
  235. protected static $_initialState = array();
  236. /**
  237. * Cache inflected values, and return if already available
  238. *
  239. * @param string $type Inflection type
  240. * @param string $key Original value
  241. * @param string $value Inflected value
  242. * @return string Inflected value, from cache
  243. */
  244. protected static function _cache($type, $key, $value = false) {
  245. $key = '_' . $key;
  246. $type = '_' . $type;
  247. if ($value !== false) {
  248. self::$_cache[$type][$key] = $value;
  249. return $value;
  250. }
  251. if (!isset(self::$_cache[$type][$key])) {
  252. return false;
  253. }
  254. return self::$_cache[$type][$key];
  255. }
  256. /**
  257. * Clears Inflectors inflected value caches. And resets the inflection
  258. * rules to the initial values.
  259. *
  260. * @return void
  261. */
  262. public static function reset() {
  263. if (empty(self::$_initialState)) {
  264. self::$_initialState = get_class_vars('Inflector');
  265. return;
  266. }
  267. foreach (self::$_initialState as $key => $val) {
  268. if ($key != '_initialState') {
  269. self::${$key} = $val;
  270. }
  271. }
  272. }
  273. /**
  274. * Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type.
  275. *
  276. * ### Usage:
  277. *
  278. * {{{
  279. * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
  280. * Inflector::rules('plural', array(
  281. * 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
  282. * 'uninflected' => array('dontinflectme'),
  283. * 'irregular' => array('red' => 'redlings')
  284. * ));
  285. * Inflector::rules('transliteration', array('/å/' => 'aa'));
  286. * }}}
  287. *
  288. * @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration'
  289. * @param array $rules Array of rules to be added.
  290. * @param boolean $reset If true, will unset default inflections for all
  291. * new rules that are being defined in $rules.
  292. * @return void
  293. */
  294. public static function rules($type, $rules, $reset = false) {
  295. $var = '_' . $type;
  296. switch ($type) {
  297. case 'transliteration':
  298. if ($reset) {
  299. self::$_transliteration = $rules;
  300. } else {
  301. self::$_transliteration = $rules + self::$_transliteration;
  302. }
  303. break;
  304. default:
  305. foreach ($rules as $rule => $pattern) {
  306. if (is_array($pattern)) {
  307. if ($reset) {
  308. self::${$var}[$rule] = $pattern;
  309. } else {
  310. if ($rule === 'uninflected') {
  311. self::${$var}[$rule] = array_merge($pattern, self::${$var}[$rule]);
  312. } else {
  313. self::${$var}[$rule] = $pattern + self::${$var}[$rule];
  314. }
  315. }
  316. unset($rules[$rule], self::${$var}['cache' . ucfirst($rule)]);
  317. if (isset(self::${$var}['merged'][$rule])) {
  318. unset(self::${$var}['merged'][$rule]);
  319. }
  320. if ($type === 'plural') {
  321. self::$_cache['pluralize'] = self::$_cache['tableize'] = array();
  322. } elseif ($type === 'singular') {
  323. self::$_cache['singularize'] = array();
  324. }
  325. }
  326. }
  327. self::${$var}['rules'] = $rules + self::${$var}['rules'];
  328. break;
  329. }
  330. }
  331. /**
  332. * Return $word in plural form.
  333. *
  334. * @param string $word Word in singular
  335. * @return string Word in plural
  336. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::pluralize
  337. */
  338. public static function pluralize($word) {
  339. if (isset(self::$_cache['pluralize'][$word])) {
  340. return self::$_cache['pluralize'][$word];
  341. }
  342. if (!isset(self::$_plural['merged']['irregular'])) {
  343. self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
  344. }
  345. if (!isset(self::$_plural['merged']['uninflected'])) {
  346. self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
  347. }
  348. if (!isset(self::$_plural['cacheUninflected']) || !isset(self::$_plural['cacheIrregular'])) {
  349. self::$_plural['cacheUninflected'] = '(?:' . implode('|', self::$_plural['merged']['uninflected']) . ')';
  350. self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
  351. }
  352. if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
  353. self::$_cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
  354. return self::$_cache['pluralize'][$word];
  355. }
  356. if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
  357. self::$_cache['pluralize'][$word] = $word;
  358. return $word;
  359. }
  360. foreach (self::$_plural['rules'] as $rule => $replacement) {
  361. if (preg_match($rule, $word)) {
  362. self::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
  363. return self::$_cache['pluralize'][$word];
  364. }
  365. }
  366. }
  367. /**
  368. * Return $word in singular form.
  369. *
  370. * @param string $word Word in plural
  371. * @return string Word in singular
  372. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::singularize
  373. */
  374. public static function singularize($word) {
  375. if (isset(self::$_cache['singularize'][$word])) {
  376. return self::$_cache['singularize'][$word];
  377. }
  378. if (!isset(self::$_singular['merged']['uninflected'])) {
  379. self::$_singular['merged']['uninflected'] = array_merge(
  380. self::$_singular['uninflected'],
  381. self::$_uninflected
  382. );
  383. }
  384. if (!isset(self::$_singular['merged']['irregular'])) {
  385. self::$_singular['merged']['irregular'] = array_merge(
  386. self::$_singular['irregular'],
  387. array_flip(self::$_plural['irregular'])
  388. );
  389. }
  390. if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
  391. self::$_singular['cacheUninflected'] = '(?:' . join( '|', self::$_singular['merged']['uninflected']) . ')';
  392. self::$_singular['cacheIrregular'] = '(?:' . join( '|', array_keys(self::$_singular['merged']['irregular'])) . ')';
  393. }
  394. if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
  395. self::$_cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
  396. return self::$_cache['singularize'][$word];
  397. }
  398. if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
  399. self::$_cache['singularize'][$word] = $word;
  400. return $word;
  401. }
  402. foreach (self::$_singular['rules'] as $rule => $replacement) {
  403. if (preg_match($rule, $word)) {
  404. self::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
  405. return self::$_cache['singularize'][$word];
  406. }
  407. }
  408. self::$_cache['singularize'][$word] = $word;
  409. return $word;
  410. }
  411. /**
  412. * Returns the given lower_case_and_underscored_word as a CamelCased word.
  413. *
  414. * @param string $lowerCaseAndUnderscoredWord Word to camelize
  415. * @return string Camelized word. LikeThis.
  416. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::camelize
  417. */
  418. public static function camelize($lowerCaseAndUnderscoredWord) {
  419. if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
  420. $result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
  421. self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
  422. }
  423. return $result;
  424. }
  425. /**
  426. * Returns the given camelCasedWord as an underscored_word.
  427. *
  428. * @param string $camelCasedWord Camel-cased word to be "underscorized"
  429. * @return string Underscore-syntaxed version of the $camelCasedWord
  430. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::underscore
  431. */
  432. public static function underscore($camelCasedWord) {
  433. if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
  434. $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
  435. self::_cache(__FUNCTION__, $camelCasedWord, $result);
  436. }
  437. return $result;
  438. }
  439. /**
  440. * Returns the given underscored_word_group as a Human Readable Word Group.
  441. * (Underscores are replaced by spaces and capitalized following words.)
  442. *
  443. * @param string $lowerCaseAndUnderscoredWord String to be made more readable
  444. * @return string Human-readable string
  445. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::humanize
  446. */
  447. public static function humanize($lowerCaseAndUnderscoredWord) {
  448. if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
  449. $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
  450. self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
  451. }
  452. return $result;
  453. }
  454. /**
  455. * Returns corresponding table name for given model $className. ("people" for the model class "Person").
  456. *
  457. * @param string $className Name of class to get database table name for
  458. * @return string Name of the database table for given class
  459. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::tableize
  460. */
  461. public static function tableize($className) {
  462. if (!($result = self::_cache(__FUNCTION__, $className))) {
  463. $result = Inflector::pluralize(Inflector::underscore($className));
  464. self::_cache(__FUNCTION__, $className, $result);
  465. }
  466. return $result;
  467. }
  468. /**
  469. * Returns Cake model class name ("Person" for the database table "people".) for given database table.
  470. *
  471. * @param string $tableName Name of database table to get class name for
  472. * @return string Class name
  473. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::classify
  474. */
  475. public static function classify($tableName) {
  476. if (!($result = self::_cache(__FUNCTION__, $tableName))) {
  477. $result = Inflector::camelize(Inflector::singularize($tableName));
  478. self::_cache(__FUNCTION__, $tableName, $result);
  479. }
  480. return $result;
  481. }
  482. /**
  483. * Returns camelBacked version of an underscored string.
  484. *
  485. * @param string $string
  486. * @return string in variable form
  487. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::variable
  488. */
  489. public static function variable($string) {
  490. if (!($result = self::_cache(__FUNCTION__, $string))) {
  491. $string2 = Inflector::camelize(Inflector::underscore($string));
  492. $replace = strtolower(substr($string2, 0, 1));
  493. $result = preg_replace('/\\w/', $replace, $string2, 1);
  494. self::_cache(__FUNCTION__, $string, $result);
  495. }
  496. return $result;
  497. }
  498. /**
  499. * Returns a string with all spaces converted to underscores (by default), accented
  500. * characters converted to non-accented characters, and non word characters removed.
  501. *
  502. * @param string $string the string you want to slug
  503. * @param string $replacement will replace keys in map
  504. * @return string
  505. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::slug
  506. */
  507. public static function slug($string, $replacement = '_') {
  508. $quotedReplacement = preg_quote($replacement, '/');
  509. $merge = array(
  510. '/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
  511. '/\\s+/' => $replacement,
  512. sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
  513. );
  514. $map = self::$_transliteration + $merge;
  515. return preg_replace(array_keys($map), array_values($map), $string);
  516. }
  517. }
  518. // Store the initial state
  519. Inflector::reset();