You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Text.php 2.8KB

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