Back to OutputFilter class

Method stringURLSafe

public static string
stringURLSafe
(mixed $string, mixed $language = '')
This method processes a string and replaces all accented UTF-8 characters by unaccented ASCII-7 "equivalents", whitespaces are replaced by hyphens and the string is lowercase.
Parameters
  • string $string String to process
  • string $language Language to transliterate to
Returns
  • string Processed string
Since
  • 1.7.0
Class: OutputFilter
Project: Joomla

Method stringURLSafe - Source code

/**
 * This method processes a string and replaces all accented UTF-8 characters by unaccented
 * ASCII-7 "equivalents", whitespaces are replaced by hyphens and the string is lowercase.
 *
 * @param   string  $string    String to process
 * @param   string  $language  Language to transliterate to
 *
 * @return  string  Processed string
 *
 * @since   1.7.0
 */
public static function stringURLSafe($string, $language = '')
{
    // Remove any '-' from the string since they will be used as concatenaters
    $str = str_replace('-', ' ', $string);
    // Transliterate on the language requested (fallback to current language if not specified)
    $lang = $language == '' || $language == '*' ? Factory::getLanguage() : Language::getInstance($language);
    $str = $lang->transliterate($str);
    // Trim white spaces at beginning and end of alias and make lowercase
    $str = trim(StringHelper::strtolower($str));
    // Remove any apostrophe. We do it here to ensure it is not replaced by a '-'
    $str = str_replace("'", '', $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, '-');
    return $str;
}