您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

modifier.capitalize.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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("!(^|[^\p{L}'])([\p{Ll}])!S" . Smarty::$_UTF8_MODIFIER,
  32. 'smarty_mod_cap_mbconvert_cb', $string);
  33. }
  34. // check uc_digits case
  35. if (!$uc_digits) {
  36. if (preg_match_all("!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER, $string, $matches,
  37. PREG_OFFSET_CAPTURE)) {
  38. foreach ($matches[ 1 ] as $match) {
  39. $upper_string =
  40. substr_replace($upper_string, mb_strtolower($match[ 0 ], Smarty::$_CHARSET), $match[ 1 ],
  41. strlen($match[ 0 ]));
  42. }
  43. }
  44. }
  45. $upper_string =
  46. preg_replace_callback("!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER, 'smarty_mod_cap_mbconvert2_cb',
  47. $upper_string);
  48. return $upper_string;
  49. }
  50. // lowercase first
  51. if ($lc_rest) {
  52. $string = strtolower($string);
  53. }
  54. // uppercase (including hyphenated words)
  55. $upper_string =
  56. preg_replace_callback("!(^|[^\p{L}'])([\p{Ll}])!S" . Smarty::$_UTF8_MODIFIER, 'smarty_mod_cap_ucfirst_cb',
  57. $string);
  58. // check uc_digits case
  59. if (!$uc_digits) {
  60. if (preg_match_all("!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER, $string, $matches,
  61. PREG_OFFSET_CAPTURE)) {
  62. foreach ($matches[ 1 ] as $match) {
  63. $upper_string =
  64. substr_replace($upper_string, strtolower($match[ 0 ]), $match[ 1 ], strlen($match[ 0 ]));
  65. }
  66. }
  67. }
  68. $upper_string = preg_replace_callback("!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER, 'smarty_mod_cap_ucfirst2_cb',
  69. $upper_string);
  70. return $upper_string;
  71. }
  72. /*
  73. *
  74. * Bug: create_function() use exhausts memory when used in long loops
  75. * Fix: use declared functions for callbacks instead of using create_function()
  76. * Note: This can be fixed using anonymous functions instead, but that requires PHP >= 5.3
  77. *
  78. * @author Kyle Renfrow
  79. */
  80. /**
  81. * @param $matches
  82. *
  83. * @return string
  84. */
  85. function smarty_mod_cap_mbconvert_cb($matches)
  86. {
  87. return stripslashes($matches[ 1 ]) . mb_convert_case(stripslashes($matches[ 2 ]), MB_CASE_UPPER, Smarty::$_CHARSET);
  88. }
  89. /**
  90. * @param $matches
  91. *
  92. * @return string
  93. */
  94. function smarty_mod_cap_mbconvert2_cb($matches)
  95. {
  96. return stripslashes($matches[ 1 ]) . mb_convert_case(stripslashes($matches[ 3 ]), MB_CASE_UPPER, Smarty::$_CHARSET);
  97. }
  98. /**
  99. * @param $matches
  100. *
  101. * @return string
  102. */
  103. function smarty_mod_cap_ucfirst_cb($matches)
  104. {
  105. return stripslashes($matches[ 1 ]) . ucfirst(stripslashes($matches[ 2 ]));
  106. }
  107. /**
  108. * @param $matches
  109. *
  110. * @return string
  111. */
  112. function smarty_mod_cap_ucfirst2_cb($matches)
  113. {
  114. return stripslashes($matches[ 1 ]) . ucfirst(stripslashes($matches[ 3 ]));
  115. }