modifier.regex_replace.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage PluginsModifier
  6. */
  7. /**
  8. * Smarty regex_replace modifier plugin
  9. *
  10. * Type: modifier<br>
  11. * Name: regex_replace<br>
  12. * Purpose: regular expression search/replace
  13. * @link http://smarty.php.net/manual/en/language.modifier.regex.replace.php
  14. * regex_replace (Smarty online manual)
  15. * @author Monte Ohrt <monte at ohrt dot com>
  16. * @param string
  17. * @param string|array
  18. * @param string|array
  19. * @return string
  20. */
  21. function smarty_modifier_regex_replace($string, $search, $replace)
  22. {
  23. if(is_array($search)) {
  24. foreach($search as $idx => $s)
  25. $search[$idx] = _smarty_regex_replace_check($s);
  26. } else {
  27. $search = _smarty_regex_replace_check($search);
  28. }
  29. return preg_replace($search, $replace, $string);
  30. }
  31. function _smarty_regex_replace_check($search)
  32. {
  33. if (($pos = strpos($search,"\0")) !== false)
  34. $search = substr($search,0,$pos);
  35. if (preg_match('!([a-zA-Z\s]+)$!s', $search, $match) && (strpos($match[1], 'e') !== false)) {
  36. /* remove eval-modifier from $search */
  37. $search = substr($search, 0, -strlen($match[1])) . preg_replace('![e\s]+!', '', $match[1]);
  38. }
  39. return $search;
  40. }
  41. ?>