/** * Changes a string into a URL-friendly string * * @param string $string The string to convert * @return void */ public static function makeFriendly($string) { $string = fHTML::decode(fUTF8::ascii($string)); $string = strtolower(trim($string)); $string = str_replace("'", '', $string); $string = preg_replace('#[^a-z0-9\\-]+#', '_', $string); $string = preg_replace('#_{2,}#', '_', $string); $string = preg_replace('#_-_#', '-', $string); return preg_replace('#(^_+|_+$)#D', '', $string); }
/** * Changes a string into a URL-friendly string * * @param string $string The string to convert * @param integer $max_length The maximum length of the friendly URL * @param string $delimiter The delimiter to use between words, defaults to `_` * @param string |$string * @param string |$delimiter * @return string The URL-friendly version of the string */ public static function makeFriendly($string, $max_length = NULL, $delimiter = NULL) { // This allows omitting the max length, but including a delimiter if ($max_length && !is_numeric($max_length)) { $delimiter = $max_length; $max_length = NULL; } $string = fHTML::decode(fUTF8::ascii($string)); $string = strtolower(trim($string)); $string = str_replace("'", '', $string); if (!strlen($delimiter)) { $delimiter = '_'; } $delimiter_replacement = strtr($delimiter, array('\\' => '\\\\', '$' => '\\$')); $delimiter_regex = preg_quote($delimiter, '#'); $string = preg_replace('#[^a-z0-9\\-_]+#', $delimiter_replacement, $string); $string = preg_replace('#' . $delimiter_regex . '{2,}#', $delimiter_replacement, $string); $string = preg_replace('#_-_#', '-', $string); $string = preg_replace('#(^' . $delimiter_regex . '+|' . $delimiter_regex . '+$)#D', '', $string); $length = strlen($string); if ($max_length && $length > $max_length) { $last_pos = strrpos($string, $delimiter, ($length - $max_length - 1) * -1); if ($last_pos < ceil($max_length / 2)) { $last_pos = $max_length; } $string = substr($string, 0, $last_pos); } return $string; }
/** * Changes a string into a URL-friendly string * * @param string $string The string to convert * @param interger $max_length The maximum length of the friendly URL * @return string The URL-friendly version of the string */ public static function makeFriendly($string, $max_length = NULL) { $string = fHTML::decode(fUTF8::ascii($string)); $string = strtolower(trim($string)); $string = str_replace("'", '', $string); $string = preg_replace('#[^a-z0-9\\-]+#', '_', $string); $string = preg_replace('#_{2,}#', '_', $string); $string = preg_replace('#_-_#', '-', $string); $string = preg_replace('#(^_+|_+$)#D', '', $string); $length = strlen($string); if ($max_length && $length > $max_length) { $last_pos = strrpos($string, '_', ($length - $max_length - 1) * -1); if ($last_pos < ceil($max_length / 2)) { $last_pos = $max_length; } $string = substr($string, 0, $last_pos); } return $string; }