Text.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /**
  11. * @author Henrik Bjornskov <hb@peytz.dk>
  12. */
  13. class Twig_Extensions_Extension_Text extends Twig_Extension
  14. {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function getFilters()
  19. {
  20. return array(
  21. new Twig_SimpleFilter('truncate', 'twig_truncate_filter', array('needs_environment' => true)),
  22. new Twig_SimpleFilter('wordwrap', 'twig_wordwrap_filter', array('needs_environment' => true)),
  23. );
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getName()
  29. {
  30. return 'Text';
  31. }
  32. }
  33. if (function_exists('mb_get_info')) {
  34. function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
  35. {
  36. if (mb_strlen($value, $env->getCharset()) > $length) {
  37. if ($preserve) {
  38. // If breakpoint is on the last word, return the value without separator.
  39. if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) {
  40. return $value;
  41. }
  42. $length = $breakpoint;
  43. }
  44. return rtrim(mb_substr($value, 0, $length, $env->getCharset())).$separator;
  45. }
  46. return $value;
  47. }
  48. function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
  49. {
  50. $sentences = array();
  51. $previous = mb_regex_encoding();
  52. mb_regex_encoding($env->getCharset());
  53. $pieces = mb_split($separator, $value);
  54. mb_regex_encoding($previous);
  55. foreach ($pieces as $piece) {
  56. while (!$preserve && mb_strlen($piece, $env->getCharset()) > $length) {
  57. $sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
  58. $piece = mb_substr($piece, $length, 2048, $env->getCharset());
  59. }
  60. $sentences[] = $piece;
  61. }
  62. return implode($separator, $sentences);
  63. }
  64. } else {
  65. function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
  66. {
  67. if (strlen($value) > $length) {
  68. if ($preserve) {
  69. if (false !== ($breakpoint = strpos($value, ' ', $length))) {
  70. $length = $breakpoint;
  71. }
  72. }
  73. return rtrim(substr($value, 0, $length)).$separator;
  74. }
  75. return $value;
  76. }
  77. function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
  78. {
  79. return wordwrap($value, $length, $separator, !$preserve);
  80. }
  81. }
  82. class_alias('Twig_Extensions_Extension_Text', 'Twig\Extensions\TextExtension', false);