Example #1
0
 static function strSplitter($str, $length = 21)
 {
     $dots = strlen($str) > $length ? '...' : '';
     return mb_substr($str, 0, $length) . $dots;
 }
Example #2
0
 /**
  * Adapted from Twig Extensions project, author: Henrik Bjornskov, (c) 2009 Fabien Potencier
  * 
  * @see <a href="https://github.com/twigphp/Twig-extensions/blob/master/lib/Twig/Extensions/Extension/Text.php">https://github.com/twigphp/Twig-extensions/blob/master/lib/Twig/Extensions/Extension/Text.php</a>
  * @param \Cantiga\CoreBundle\Twig\Twig_Environment $env
  * @param string $value
  * @param int $length
  * @param boolean $preserve
  * @param string $separator
  * @return string
  */
 public function truncate(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
 {
     if (mb_strlen($value, $env->getCharset()) > $length) {
         if ($preserve) {
             // If breakpoint is on the last word, return the value without separator.
             if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) {
                 return $value;
             }
             $length = $breakpoint;
         }
         return rtrim(mb_substr($value, 0, $length, $env->getCharset())) . $separator;
     }
     return $value;
 }