PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/include/phpgacl/admin/smarty/libs/plugins/outputfilter.trimwhitespace.php

https://github.com/radicaldesigns/amp
PHP | 75 lines | 31 code | 11 blank | 33 comment | 2 complexity | d450a6b8021bd85d8a01dda02e666dc3 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty trimwhitespace outputfilter plugin
  9. *
  10. * File: outputfilter.trimwhitespace.php<br>
  11. * Type: outputfilter<br>
  12. * Name: trimwhitespace<br>
  13. * Date: Jan 25, 2003<br>
  14. * Purpose: trim leading white space and blank lines from
  15. * template source after it gets interpreted, cleaning
  16. * up code and saving bandwidth. Does not affect
  17. * <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br>
  18. * Install: Drop into the plugin directory, call
  19. * <code>$smarty->load_filter('output','trimwhitespace');</code>
  20. * from application.
  21. * @author Monte Ohrt <monte at ohrt dot com>
  22. * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
  23. * @version 1.3
  24. * @param string
  25. * @param Smarty
  26. */
  27. function smarty_outputfilter_trimwhitespace($source, &$smarty)
  28. {
  29. // Pull out the script blocks
  30. preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match);
  31. $_script_blocks = $match[0];
  32. $source = preg_replace("!<script[^>]+>.*?</script>!is",
  33. '@@@SMARTY:TRIM:SCRIPT@@@', $source);
  34. // Pull out the pre blocks
  35. preg_match_all("!<pre>.*?</pre>!is", $source, $match);
  36. $_pre_blocks = $match[0];
  37. $source = preg_replace("!<pre>.*?</pre>!is",
  38. '@@@SMARTY:TRIM:PRE@@@', $source);
  39. // Pull out the textarea blocks
  40. preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match);
  41. $_textarea_blocks = $match[0];
  42. $source = preg_replace("!<textarea[^>]+>.*?</textarea>!is",
  43. '@@@SMARTY:TRIM:TEXTAREA@@@', $source);
  44. // remove all leading spaces, tabs and carriage returns NOT
  45. // preceeded by a php close tag.
  46. $source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source));
  47. // replace script blocks
  48. smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:SCRIPT@@@",$_script_blocks, $source);
  49. // replace pre blocks
  50. smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:PRE@@@",$_pre_blocks, $source);
  51. // replace textarea blocks
  52. smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:TEXTAREA@@@",$_textarea_blocks, $source);
  53. return $source;
  54. }
  55. function smarty_outputfilter_trimwhitespace_replace($search_str, $replace, &$subject) {
  56. $_len = strlen($search_str);
  57. $_pos = 0;
  58. for ($_i=0, $_count=count($replace); $_i<$_count; $_i++)
  59. if (($_pos=strpos($subject, $search_str, $_pos))!==false)
  60. $subject = substr_replace($subject, $replace[$_i], $_pos, $_len);
  61. else
  62. break;
  63. }
  64. ?>