Skip to content

Commit

Permalink
Updated URL::title to accept any character as word separator.
Browse files Browse the repository at this point in the history
This makes the function a bit more flexible. For example, you could use a dot as separator to generate usernames like "geert.de.deckere".
  • Loading branch information
Geert De Deckere committed Nov 30, 2009
1 parent e2d4d66 commit 3597a8c
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions classes/kohana/url.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,28 @@ public static function query(array $params = NULL)
* Convert a phrase to a URL-safe title.
*
* @param string phrase to convert
* @param string word separator (- or _)
* @param string word separator (any single character)
* @param boolean transliterate to ASCII
* @return string
*/
public static function title($title, $separator = '-', $ascii_only = FALSE)
{
$separator = ($separator === '-') ? '-' : '_';

if ($ascii_only === TRUE)
{
// Transliterate non-ASCII characters
$title = UTF8::transliterate_to_ascii($title);

// Remove all characters that are not the separator, a-z, 0-9, or whitespace
$title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
$title = preg_replace('![^'.preg_quote($separator).'a-z0-9\s]+!', '', strtolower($title));
}
else
{
// Remove all characters that are not the separator, letters, numbers, or whitespace
$title = preg_replace('/[^'.$separator.'\pL\pN\s]+/u', '', UTF8::strtolower($title));
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', UTF8::strtolower($title));
}

// Replace all separator characters and whitespace by a single separator
$title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);

// Trim separators from the beginning and end
return trim($title, $separator);
Expand Down

1 comment on commit 3597a8c

@shadowhand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good choice, Geert.

Please sign in to comment.