Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tobias-forkel/ff19ee83f0c25c4542d8 to your computer and use it in GitHub Desktop.
Save tobias-forkel/ff19ee83f0c25c4542d8 to your computer and use it in GitHub Desktop.
Cut the text after a specific number of characters. If the max length is not reached, just output the text without modifications.
<?php
/**
* Namespace ModuleName
*
* @category Namespace
* @package Namespace_ModuleName
* @copyright Copyright (c) 2015 Tobias Forkel (http://www.tobiasforkel.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Namespace_ModuleName_Helper_Data extends Mage_Core_Helper_Abstract
{
/**
* Cut the text after a specific number of characters.
* If the max length is not reached, just output the text without modifications.
*
* If a word is longer then the max length, it will not work properly. Please extend this method
* to support strings without spaces.
*
* @param string $string Any kind of string.
* @param integer $maxlength The max length to output.
*
* @return string
*/
public function truncateText($string = '', $maxlength = 10)
{
return strtok(wordwrap($string, $maxlength, ' ...\n'), '\n');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment