Added string method to generate a URL slug

This commit is contained in:
Josh Sherman 2012-11-13 22:40:09 -05:00
parent de89ef85d1
commit dbcf427616
2 changed files with 42 additions and 0 deletions

View file

@ -64,6 +64,27 @@ class String
} }
// }}} // }}}
// {{{ Generate Slug
/**
* Generate Slug
*
* Generates a slug from the pass string by lowercasing the string, trimming
* whitespace and converting non-alphanumeric values to dashes. Takes care
* of multiple dashes as well.
*
* @static
* @param string $string to be converted to the slug
* @return string resulting slug
*/
public static function generateSlug($string)
{
$string = strtolower(trim($string));
$string = preg_replace('/[^a-z0-9-]/', '-', $string);
$string = preg_replace('/-+/', '-', $string);
return trim($string, '-');;
}
// {{{ Is Empty // {{{ Is Empty
/** /**

21
jar.php
View file

@ -7057,6 +7057,27 @@ class String
} }
// }}} // }}}
// {{{ Generate Slug
/**
* Generate Slug
*
* Generates a slug from the pass string by lowercasing the string, trimming
* whitespace and converting non-alphanumeric values to dashes. Takes care
* of multiple dashes as well.
*
* @static
* @param string $string to be converted to the slug
* @return string resulting slug
*/
public static function generateSlug($string)
{
$string = strtolower(trim($string));
$string = preg_replace('/[^a-z0-9-]/', '-', $string);
$string = preg_replace('/-+/', '-', $string);
return trim($string, '-');;
}
// {{{ Is Empty // {{{ Is Empty
/** /**