PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/bblog/smarty_plugins/outputfilter.trimwhitespace.php

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