Skip to content

Instantly share code, notes, and snippets.

@dsalvagni
Created May 7, 2014 18:58
Show Gist options
  • Save dsalvagni/a6aaec3dd4888e2bff12 to your computer and use it in GitHub Desktop.
Save dsalvagni/a6aaec3dd4888e2bff12 to your computer and use it in GitHub Desktop.
ZF2 - TextShortener ViewHelper
<?php
/* Beggining of your file */
use Application\View\Helper\TextShortener;
public function getViewHelperConfig()
{
return array(
'factories' => array(
'TextShortener' => function($sm) {
$locator = $sm->getServiceLocator();
return new TextShortener();
},
),
);
}
<?php
/**
* TextShortener - ZF2 ViewHelper
* $this->TextShortener('Text',10,100,'...');
*
* @autor Daniel Salvagni <[email protected]>
*/
/* Use your namespace */
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class TextShortener extends AbstractHelper
{
/**
* Will truncate the string with both words and chars count
*/
protected function both($text, $words, $chars)
{
$explodeWords = preg_split('/[\s,]+/', $text);
if(!count($explodeWords)) return substr($text,0,$chars);
$newText = "";
$i = 1;
foreach($explodeWords as $word):
if((strlen($newText)<$chars) || ($i < $words))
$newText .= " {$word}";
else break;
$i++;
endforeach;
return $newText;
}
/**
* Will truncate the string with the words count
*/
protected function words($text, $words)
{
return preg_replace('/((\w+\W*){'.$words.'}(\w+))(.*)/', '${1}', $text);
}
/**
* Will truncate the string with the chars count
*/
protected function chars($text, $chars)
{
if(strlen($text) < $chars) return $text;
else return substr($text, 0, strpos(substr($text, 0, $chars), ' '));
}
/**
* Invoke the class as a function to execute the actions
*/
public function __invoke($text, $words=false, $chars=false, $end="...")
{
if($words && $chars):
$string = $this->both($text,$words,$chars);
elseif($words && !$chars):
$string = $this->words($text,$chars);
elseif($chars && !$words):
$string = $this->chars($text,$chars);
else:
$string = $text;
endif;
$endChar = (!$end) ? null : $end;
return ($string != $text) ? rtrim($string,'.,;:?!¿¡').$endChar : $string ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment