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

/code/web/private_php/ams/smarty/libs/plugins/modifier.capitalize.php

https://bitbucket.org/kkszysiu/ryzomcore
PHP | 90 lines | 54 code | 5 blank | 31 comment | 3 complexity | 35d7def91b1cfa8ec44facce157005a9 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsModifier
  7. */
  8. /**
  9. * Smarty capitalize modifier plugin
  10. * Type: modifier<br>
  11. * Name: capitalize<br>
  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, 'smarty_mod_cap_mbconvert_cb', $string);
  32. }
  33. // check uc_digits case
  34. if (!$uc_digits) {
  35. if (preg_match_all("!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER, $string, $matches, PREG_OFFSET_CAPTURE)) {
  36. foreach ($matches[1] as $match) {
  37. $upper_string = substr_replace($upper_string, mb_strtolower($match[0], Smarty::$_CHARSET), $match[1], strlen($match[0]));
  38. }
  39. }
  40. }
  41. $upper_string = preg_replace_callback("!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER, 'smarty_mod_cap_mbconvert2_cb', $upper_string);
  42. return $upper_string;
  43. }
  44. // lowercase first
  45. if ($lc_rest) {
  46. $string = strtolower($string);
  47. }
  48. // uppercase (including hyphenated words)
  49. $upper_string = preg_replace_callback("!(^|[^\p{L}'])([\p{Ll}])!S" . Smarty::$_UTF8_MODIFIER, 'smarty_mod_cap_ucfirst_cb', $string);
  50. // check uc_digits case
  51. if (!$uc_digits) {
  52. if (preg_match_all("!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER, $string, $matches, PREG_OFFSET_CAPTURE)) {
  53. foreach ($matches[1] as $match) {
  54. $upper_string = substr_replace($upper_string, strtolower($match[0]), $match[1], strlen($match[0]));
  55. }
  56. }
  57. }
  58. $upper_string = preg_replace_callback("!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER, 'smarty_mod_cap_ucfirst2_cb', $upper_string);
  59. return $upper_string;
  60. }
  61. /*
  62. *
  63. * Bug: create_function() use exhausts memory when used in long loops
  64. * Fix: use declared functions for callbacks instead of using create_function()
  65. * Note: This can be fixed using anonymous functions instead, but that requires PHP >= 5.3
  66. *
  67. * @author Kyle Renfrow
  68. */
  69. function smarty_mod_cap_mbconvert_cb($matches)
  70. {
  71. return stripslashes($matches[1]) . mb_convert_case(stripslashes($matches[2]), MB_CASE_UPPER, Smarty::$_CHARSET);
  72. }
  73. function smarty_mod_cap_mbconvert2_cb($matches)
  74. {
  75. return stripslashes($matches[1]) . mb_convert_case(stripslashes($matches[3]), MB_CASE_UPPER, Smarty::$_CHARSET);
  76. }
  77. function smarty_mod_cap_ucfirst_cb($matches)
  78. {
  79. return stripslashes($matches[1]) . ucfirst(stripslashes($matches[2]));
  80. }
  81. function smarty_mod_cap_ucfirst2_cb($matches)
  82. {
  83. return stripslashes($matches[1]) . ucfirst(stripslashes($matches[3]));
  84. }