Text.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $filters = 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. );
  27. if (version_compare(Twig_Environment::VERSION, '1.5.0-DEV', '<')) {
  28. $filters['nl2br'] = new Twig_Filter_Function('twig_nl2br_filter', array('pre_escape' => 'html', 'is_safe' => array('html')));
  29. }
  30. return $filters;
  31. }
  32. /**
  33. * Name of this extension
  34. *
  35. * @return string
  36. */
  37. public function getName()
  38. {
  39. return 'Text';
  40. }
  41. }
  42. function twig_nl2br_filter($value, $sep = '<br />')
  43. {
  44. return str_replace("\n", $sep."\n", $value);
  45. }
  46. if (function_exists('mb_get_info')) {
  47. function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
  48. {
  49. if (mb_strlen($value, $env->getCharset()) > $length) {
  50. if ($preserve) {
  51. if (false !== ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) {
  52. $length = $breakpoint;
  53. }
  54. }
  55. return mb_substr($value, 0, $length, $env->getCharset()) . $separator;
  56. }
  57. return $value;
  58. }
  59. function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
  60. {
  61. $sentences = array();
  62. $previous = mb_regex_encoding();
  63. mb_regex_encoding($env->getCharset());
  64. $pieces = mb_split($separator, $value);
  65. mb_regex_encoding($previous);
  66. foreach ($pieces as $piece) {
  67. while(!$preserve && mb_strlen($piece, $env->getCharset()) > $length) {
  68. $sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
  69. $piece = mb_substr($piece, $length, 2048, $env->getCharset());
  70. }
  71. $sentences[] = $piece;
  72. }
  73. return implode($separator, $sentences);
  74. }
  75. } else {
  76. function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
  77. {
  78. if (strlen($value) > $length) {
  79. if ($preserve) {
  80. if (false !== ($breakpoint = strpos($value, ' ', $length))) {
  81. $length = $breakpoint;
  82. }
  83. }
  84. return substr($value, 0, $length) . $separator;
  85. }
  86. return $value;
  87. }
  88. function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
  89. {
  90. return wordwrap($value, $length, $separator, !$preserve);
  91. }
  92. }