shared.escape_special_chars.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Smarty shared plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsShared
  7. */
  8. if (version_compare(PHP_VERSION, '5.2.3', '>=')) {
  9. /**
  10. * escape_special_chars common function
  11. *
  12. * Function: smarty_function_escape_special_chars<br>
  13. * Purpose: used by other smarty functions to escape
  14. * special chars except for already escaped ones
  15. *
  16. * @author Monte Ohrt <monte at ohrt dot com>
  17. * @param string $string text that should by escaped
  18. * @return string
  19. */
  20. function smarty_function_escape_special_chars($string)
  21. {
  22. if (!is_array($string)) {
  23. $string = htmlspecialchars($string, ENT_COMPAT, Smarty::$_CHARSET, false);
  24. }
  25. return $string;
  26. }
  27. } else {
  28. /**
  29. * escape_special_chars common function
  30. *
  31. * Function: smarty_function_escape_special_chars<br>
  32. * Purpose: used by other smarty functions to escape
  33. * special chars except for already escaped ones
  34. *
  35. * @author Monte Ohrt <monte at ohrt dot com>
  36. * @param string $string text that should by escaped
  37. * @return string
  38. */
  39. function smarty_function_escape_special_chars($string)
  40. {
  41. if (!is_array($string)) {
  42. $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
  43. $string = htmlspecialchars($string);
  44. $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
  45. }
  46. return $string;
  47. }
  48. }
  49. ?>