From 906f217dc1b885cf17848b5ec9e7f1d2ffd3864e Mon Sep 17 00:00:00 2001 From: Glynn Forrest Date: Tue, 26 Nov 2013 14:11:41 +0000 Subject: [PATCH] Adding optional $replacement parameter to slugify. --- src/Stringy/StaticStringy.php | 10 ++++++---- src/Stringy/Stringy.php | 10 ++++++---- tests/Stringy/CommonTest.php | 4 +++- tests/Stringy/StaticStringyTest.php | 4 ++-- tests/Stringy/StringyTest.php | 4 ++-- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index 964137f..ba09b23 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -326,14 +326,16 @@ class StaticStringy * Converts the string into an URL slug. This includes replacing non-ASCII * characters with their closest ASCII equivalents, removing non-alphanumeric * and non-ASCII characters, and replacing whitespace with dashes. The string - * is also converted to lowercase. + * is also converted to lowercase. If defined, whitespace is replaced with + * $replacement instead of dashes. * - * @param string $str Text to transform into an URL slug + * @param string $str Text to transform into an URL slug + * @param string $replacement The string to replace whitespace with * @return string The corresponding URL slug */ - public static function slugify($str) + public static function slugify($str, $replacement = '-') { - return Stringy::create($str)->slugify()->str; + return Stringy::create($str)->slugify($replacement)->str; } /** diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index d217181..5ec7ee4 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -578,17 +578,19 @@ class Stringy * Converts the string into an URL slug. This includes replacing non-ASCII * characters with their closest ASCII equivalents, removing non-alphanumeric * and non-ASCII characters, and replacing whitespace with dashes. The string - * is also converted to lowercase. + * is also converted to lowercase. If defined, whitespace is replaced with + * $replacement instead of dashes. * + * @param string $replacement The string to replace whitespace with * @return Stringy Object whose $str has been converted to an URL slug */ - public function slugify() + public function slugify($replacement = '-') { $stringy = self::create($this->str, $this->encoding); - $stringy->str = preg_replace('/[^a-zA-Z\d -]/u', '', $stringy->toAscii()); + $stringy->str = preg_replace("/[^a-zA-Z\d $replacement]/u", '', $stringy->toAscii()); $stringy->str = $stringy->collapseWhitespace()->str; - $stringy->str = str_replace(' ', '-', strtolower($stringy->str)); + $stringy->str = str_replace(' ', $replacement, strtolower($stringy->str)); return $stringy; } diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index 35a7863..fe3d69e 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -330,7 +330,9 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase array('unrecognized-chars-like', 'unrecognized chars like συγγρ'), array('numbers-1234', 'numbers 1234'), array('perevirka-ryadka', 'перевірка рядка'), - array('bukvar-s-bukvoy-y', 'букварь с буквой ы') + array('bukvar-s-bukvoy-y', 'букварь с буквой ы'), + array('foo:bar:baz', 'Foo bar baz', ':'), + array('a_string_with_underscores', 'A_string with_underscores', '_') ); } diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index f638e21..9083ebd 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -245,9 +245,9 @@ class StaticStringyTestCase extends CommonTest /** * @dataProvider slugifyProvider() */ - public function testSlugify($expected, $str) + public function testSlugify($expected, $str, $replacement = '-') { - $result = S::slugify($str); + $result = S::slugify($str, $replacement); $this->assertInternalType('string', $result); $this->assertEquals($expected, $result); } diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 1fca907..c1e57d2 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -312,10 +312,10 @@ class StringyTestCase extends CommonTest /** * @dataProvider slugifyProvider() */ - public function testSlugify($expected, $str) + public function testSlugify($expected, $str, $replacement = '-') { $stringy = S::create($str); - $result = $stringy->slugify(); + $result = $stringy->slugify($replacement); $this->assertInstanceOf('Stringy\Stringy', $result); $this->assertEquals($expected, $result); $this->assertEquals($str, $stringy);