modifier.capitalize.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsModifier
  7. */
  8. /**
  9. * Smarty capitalize modifier plugin
  10. *
  11. * Type: modifier<br>
  12. * Name: capitalize<br>
  13. * Purpose: capitalize words in the string
  14. *
  15. * {@internal {$string|capitalize:true:true} is the fastest option for MBString enabled systems }}
  16. *
  17. * @param string $string string to capitalize
  18. * @param boolean $uc_digits also capitalize "x123" to "X123"
  19. * @param boolean $lc_rest capitalize first letters, lowercase all following letters "aAa" to "Aaa"
  20. * @return string capitalized string
  21. * @author Monte Ohrt <monte at ohrt dot com>
  22. * @author Rodney Rehm
  23. */
  24. function smarty_modifier_capitalize($string, $uc_digits = false, $lc_rest = false)
  25. {
  26. if (Smarty::$_MBSTRING) {
  27. if ($lc_rest) {
  28. // uppercase (including hyphenated words)
  29. $upper_string = mb_convert_case( $string, MB_CASE_TITLE, Smarty::$_CHARSET );
  30. } else {
  31. // uppercase word breaks
  32. $upper_string = preg_replace("!(^|[^\p{L}'])([\p{Ll}])!eS" . Smarty::$_UTF8_MODIFIER, "stripslashes('\\1').mb_convert_case(stripslashes('\\2'),MB_CASE_UPPER, '" . addslashes(Smarty::$_CHARSET) . "')", $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, PREG_OFFSET_CAPTURE)) {
  37. foreach($matches[1] as $match) {
  38. $upper_string = substr_replace($upper_string, mb_strtolower($match[0], Smarty::$_CHARSET), $match[1], strlen($match[0]));
  39. }
  40. }
  41. }
  42. $upper_string = preg_replace("!((^|\s)['\"])(\w)!e" . Smarty::$_UTF8_MODIFIER, "stripslashes('\\1').mb_convert_case(stripslashes('\\3'),MB_CASE_UPPER, '" . addslashes(Smarty::$_CHARSET) . "')", $upper_string);
  43. return $upper_string;
  44. }
  45. // lowercase first
  46. if ($lc_rest) {
  47. $string = strtolower($string);
  48. }
  49. // uppercase (including hyphenated words)
  50. $upper_string = preg_replace("!(^|[^\p{L}'])([\p{Ll}])!eS" . Smarty::$_UTF8_MODIFIER, "stripslashes('\\1').ucfirst(stripslashes('\\2'))", $string);
  51. // check uc_digits case
  52. if (!$uc_digits) {
  53. if (preg_match_all("!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER, $string, $matches, PREG_OFFSET_CAPTURE)) {
  54. foreach($matches[1] as $match) {
  55. $upper_string = substr_replace($upper_string, strtolower($match[0]), $match[1], strlen($match[0]));
  56. }
  57. }
  58. }
  59. $upper_string = preg_replace("!((^|\s)['\"])(\w)!e" . Smarty::$_UTF8_MODIFIER, "stripslashes('\\1').strtoupper(stripslashes('\\3'))", $upper_string);
  60. return $upper_string;
  61. }
  62. ?>