Tuesday, September 29, 2015

Create Slug


If you want to make a string seo friendly slug in joomla then copy and paste the following function


public function createSlug($name) {
$jinput = JFactory::getApplication()->input;
$template = $jinput->get('template', null, null);

// Transliterate
$lang = new JLanguage($template->get('language', 'general', '', null, 0, false));
$str = $lang->transliterate($name);

// Trim white spaces at beginning and end of alias and make lowercase
$str = trim(JString::strtolower($str));

// Remove any duplicate whitespace, and ensure all characters are alphanumeric
$str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str);

// Trim dashes at beginning and end of alias
$str = trim($str, '-');

// If we are left with an empty string, make a date with random number
if (trim(str_replace('-', '', $str)) == '') {
$jdate = JFactory::getDate();
$str = $jdate->format("Y-m-d-h-i-s").mt_rand();
}
return $str;
}