Text.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. *
  10. * @author Henrik Bjornskov <hb@peytz.dk>
  11. * @package Twig
  12. * @subpackage Twig-extensions
  13. */
  14. class Twig_Extensions_Extension_Text extends Twig_Extension
  15. {
  16. /**
  17. * Returns a list of filters.
  18. *
  19. * @return array
  20. */
  21. public function getFilters()
  22. {
  23. return array(
  24. 'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => true)),
  25. 'wordwrap' => new Twig_Filter_Function('twig_wordwrap_filter', array('needs_environment' => true)),
  26. 'nl2br' => new Twig_Filter_Function('twig_nl2br_filter', array('pre_escape' => 'html', 'is_safe' => array('html'))),
  27. );
  28. }
  29. /**
  30. * Name of this extension
  31. *
  32. * @return string
  33. */
  34. public function getName()
  35. {
  36. return 'Text';
  37. }
  38. }
  39. function twig_nl2br_filter($value, $sep = '<br />')
  40. {
  41. return str_replace("\n", $sep."\n", $value);
  42. }
  43. if (function_exists('mb_get_info')) {
  44. function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
  45. {
  46. if (mb_strlen($value, $env->getCharset()) > $length) {
  47. if ($preserve) {
  48. if (false !== ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) {
  49. $length = $breakpoint;
  50. }
  51. }
  52. return mb_substr($value, 0, $length, $env->getCharset()) . $separator;
  53. }
  54. return $value;
  55. }
  56. function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
  57. {
  58. $sentences = array();
  59. $previous = mb_regex_encoding();
  60. mb_regex_encoding($env->getCharset());
  61. $pieces = mb_split($separator, $value);
  62. mb_regex_encoding($previous);
  63. foreach ($pieces as $piece) {
  64. while(!$preserve && mb_strlen($piece, $env->getCharset()) > $length) {
  65. $sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
  66. $piece = mb_substr($piece, $length, 2048, $env->getCharset());
  67. }
  68. $sentences[] = $piece;
  69. }
  70. return implode($separator, $sentences);
  71. }
  72. } else {
  73. function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
  74. {
  75. if (strlen($value) > $length) {
  76. if ($preserve) {
  77. if (false !== ($breakpoint = strpos($value, ' ', $length))) {
  78. $length = $breakpoint;
  79. }
  80. }
  81. return substr($value, 0, $length) . $separator;
  82. }
  83. return $value;
  84. }
  85. function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
  86. {
  87. return wordwrap($value, $length, $separator, !$preserve);
  88. }
  89. }