PHP:
<?php
/**
* Radcodes - SocialEngine Module
*
* @category Application_Extensions
* @package Radcodes
* @copyright Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
* @license http://www.radcodes.com/license/
* @version $Id$
* @author Vincent Van <vincent@radcodes.com>
*/
class Radcodes_Lib_Helper_Text
{
/**
* Truncates +text+ to the length of +length+ and replaces the last three characters with the +truncate_string+
* if the +text+ is longer than +length+.
*/
static public function truncate($text, $length = 30, $truncate_string = '...', $truncate_lastspace = false)
{
if ($text == '')
{
return '';
}
$mbstring = extension_loaded('mbstring');
if($mbstring)
{
$old_encoding = mb_internal_encoding();
@mb_internal_encoding(mb_detect_encoding($text));
}
$strlen = ($mbstring) ? 'mb_strlen' : 'strlen';
$substr = ($mbstring) ? 'mb_substr' : 'substr';
if ($strlen($text) > $length)
{
$truncate_text = $substr($text, 0, $length - $strlen($truncate_string));
if ($truncate_lastspace)
{
$truncate_text = preg_replace('/\s+?(\S+)?$/', '', $truncate_text);
}
$text = $truncate_text.$truncate_string;
}
if($mbstring)
{
@mb_internal_encoding($old_encoding);
}
return $text;
}
/**
* Word wrap long lines to line_width.
*/
static public function wrap($text, $line_width = 80)
{
return preg_replace('/(.{1,'.$line_width.'})(\s+|$)/s', "\\1\n", preg_replace("/\n/", "\n\n", $text));
}
static public function slugify($text, $options = array())
{
$text = strtr($text, array(
"ğ"=>"g", "ü"=>"u", "ş"=>"s", "ı"=>"i", "ö"=>"o", "ç"=>"c", "Ğ"=>"g", "Ü"=>"u", "Ş"=>"s", "İ"=>"i", "Ö"=>"o", "Ç"=>"c",
"Є"=>"YE", "є"=>"ye", "Ї"=>"YI", "ї"=>"yi", "Ѓ"=>"G", "ѓ"=>"g", "№"=>"#",
"А"=>"A","Б"=>"B","В"=>"V","Г"=>"G","Д"=>"D",
"Е"=>"E","Ё"=>"YO","Ж"=>"ZH",
"З"=>"Z","И"=>"I","Й"=>"Y","К"=>"K","Л"=>"L",
"М"=>"M","Н"=>"N","О"=>"O","П"=>"P","Р"=>"R",
"С"=>"S","Т"=>"T","У"=>"U","Ф"=>"F","Х"=>"X",
"Ц"=>"TS","Ч"=>"CH","Ш"=>"SH","Щ"=>"SCH","Ъ"=>"",
"Ы"=>"I","Ь"=>"","Э"=>"E","Ю"=>"YU","Я"=>"YA",
"а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d",
"е"=>"e","ё"=>"yo","ж"=>"zh",
"з"=>"z","и"=>"i", "й"=>"y","к"=>"k","л"=>"l",
"м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
"с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"x",
"ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"",
"ы"=>"i","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya","«"=>"","»"=>"","—"=>"-"
));
// replace all non letters or digits by -
$text = preg_replace('/\W+/', '-', $text);
// trim and lowercase
$text = strtolower(trim($text, '-'));
if (empty($text))
{
return 'n-a';
}
return $text;
}
}