From dbcf427616ca8d87811bf7ccb8ab9e0d3d64f7aa Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Tue, 13 Nov 2012 22:40:09 -0500 Subject: [PATCH] Added string method to generate a URL slug --- classes/String.php | 21 +++++++++++++++++++++ jar.php | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/classes/String.php b/classes/String.php index ac1498b..861c5d9 100644 --- a/classes/String.php +++ b/classes/String.php @@ -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 /** diff --git a/jar.php b/jar.php index 551ccc0..b328a8a 100755 --- a/jar.php +++ b/jar.php @@ -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 /**