PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/core/model/smarty/plugins/modifier.capitalize.php

http://github.com/modxcms/revolution
PHP | 145 lines | 90 code | 4 blank | 51 comment | 3 complexity | 2b2261774405bd2771957e04be00056f MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsModifier
  7. */
  8. /**
  9. * Smarty capitalize modifier plugin
  10. * Type: modifier
  11. * Name: capitalize
  12. * Purpose: capitalize words in the string
  13. * {@internal {$string|capitalize:true:true} is the fastest option for MBString enabled systems }}
  14. *
  15. * @param string $string string to capitalize
  16. * @param boolean $uc_digits also capitalize "x123" to "X123"
  17. * @param boolean $lc_rest capitalize first letters, lowercase all following letters "aAa" to "Aaa"
  18. *
  19. * @return string capitalized string
  20. * @author Monte Ohrt <monte at ohrt dot com>
  21. * @author Rodney Rehm
  22. */
  23. function smarty_modifier_capitalize($string, $uc_digits = false, $lc_rest = false)
  24. {
  25. if (Smarty::$_MBSTRING) {
  26. if ($lc_rest) {
  27. // uppercase (including hyphenated words)
  28. $upper_string = mb_convert_case($string, MB_CASE_TITLE, Smarty::$_CHARSET);
  29. } else {
  30. // uppercase word breaks
  31. $upper_string = preg_replace_callback(
  32. "!(^|[^\p{L}'])([\p{Ll}])!S" . Smarty::$_UTF8_MODIFIER,
  33. 'smarty_mod_cap_mbconvert_cb',
  34. $string
  35. );
  36. }
  37. // check uc_digits case
  38. if (!$uc_digits) {
  39. if (preg_match_all(
  40. "!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER,
  41. $string,
  42. $matches,
  43. PREG_OFFSET_CAPTURE
  44. )
  45. ) {
  46. foreach ($matches[ 1 ] as $match) {
  47. $upper_string =
  48. substr_replace(
  49. $upper_string,
  50. mb_strtolower($match[ 0 ], Smarty::$_CHARSET),
  51. $match[ 1 ],
  52. strlen($match[ 0 ])
  53. );
  54. }
  55. }
  56. }
  57. $upper_string =
  58. preg_replace_callback(
  59. "!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER,
  60. 'smarty_mod_cap_mbconvert2_cb',
  61. $upper_string
  62. );
  63. return $upper_string;
  64. }
  65. // lowercase first
  66. if ($lc_rest) {
  67. $string = strtolower($string);
  68. }
  69. // uppercase (including hyphenated words)
  70. $upper_string =
  71. preg_replace_callback(
  72. "!(^|[^\p{L}'])([\p{Ll}])!S" . Smarty::$_UTF8_MODIFIER,
  73. 'smarty_mod_cap_ucfirst_cb',
  74. $string
  75. );
  76. // check uc_digits case
  77. if (!$uc_digits) {
  78. if (preg_match_all(
  79. "!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER,
  80. $string,
  81. $matches,
  82. PREG_OFFSET_CAPTURE
  83. )
  84. ) {
  85. foreach ($matches[ 1 ] as $match) {
  86. $upper_string =
  87. substr_replace($upper_string, strtolower($match[ 0 ]), $match[ 1 ], strlen($match[ 0 ]));
  88. }
  89. }
  90. }
  91. $upper_string = preg_replace_callback(
  92. "!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER,
  93. 'smarty_mod_cap_ucfirst2_cb',
  94. $upper_string
  95. );
  96. return $upper_string;
  97. }
  98. /**
  99. *
  100. * Bug: create_function() use exhausts memory when used in long loops
  101. * Fix: use declared functions for callbacks instead of using create_function()
  102. * Note: This can be fixed using anonymous functions instead, but that requires PHP >= 5.3
  103. *
  104. * @author Kyle Renfrow
  105. */
  106. /**
  107. * @param $matches
  108. *
  109. * @return string
  110. */
  111. function smarty_mod_cap_mbconvert_cb($matches)
  112. {
  113. return stripslashes($matches[ 1 ]) . mb_convert_case(stripslashes($matches[ 2 ]), MB_CASE_UPPER, Smarty::$_CHARSET);
  114. }
  115. /**
  116. * @param $matches
  117. *
  118. * @return string
  119. */
  120. function smarty_mod_cap_mbconvert2_cb($matches)
  121. {
  122. return stripslashes($matches[ 1 ]) . mb_convert_case(stripslashes($matches[ 3 ]), MB_CASE_UPPER, Smarty::$_CHARSET);
  123. }
  124. /**
  125. * @param $matches
  126. *
  127. * @return string
  128. */
  129. function smarty_mod_cap_ucfirst_cb($matches)
  130. {
  131. return stripslashes($matches[ 1 ]) . ucfirst(stripslashes($matches[ 2 ]));
  132. }
  133. /**
  134. * @param $matches
  135. *
  136. * @return string
  137. */
  138. function smarty_mod_cap_ucfirst2_cb($matches)
  139. {
  140. return stripslashes($matches[ 1 ]) . ucfirst(stripslashes($matches[ 3 ]));
  141. }