1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 16:24:00 +02:00

Adding optional $replacement parameter to slugify.

This commit is contained in:
Glynn Forrest
2013-11-26 14:11:41 +00:00
parent d4baab7583
commit 906f217dc1
5 changed files with 19 additions and 13 deletions

View File

@@ -326,14 +326,16 @@ class StaticStringy
* Converts the string into an URL slug. This includes replacing non-ASCII * Converts the string into an URL slug. This includes replacing non-ASCII
* characters with their closest ASCII equivalents, removing non-alphanumeric * characters with their closest ASCII equivalents, removing non-alphanumeric
* and non-ASCII characters, and replacing whitespace with dashes. The string * 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 * @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;
} }
/** /**

View File

@@ -578,17 +578,19 @@ class Stringy
* Converts the string into an URL slug. This includes replacing non-ASCII * Converts the string into an URL slug. This includes replacing non-ASCII
* characters with their closest ASCII equivalents, removing non-alphanumeric * characters with their closest ASCII equivalents, removing non-alphanumeric
* and non-ASCII characters, and replacing whitespace with dashes. The string * 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 * @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 = 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 = $stringy->collapseWhitespace()->str;
$stringy->str = str_replace(' ', '-', strtolower($stringy->str)); $stringy->str = str_replace(' ', $replacement, strtolower($stringy->str));
return $stringy; return $stringy;
} }

View File

@@ -330,7 +330,9 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('unrecognized-chars-like', 'unrecognized chars like συγγρ'), array('unrecognized-chars-like', 'unrecognized chars like συγγρ'),
array('numbers-1234', 'numbers 1234'), array('numbers-1234', 'numbers 1234'),
array('perevirka-ryadka', 'перевірка рядка'), 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', '_')
); );
} }

View File

@@ -245,9 +245,9 @@ class StaticStringyTestCase extends CommonTest
/** /**
* @dataProvider slugifyProvider() * @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->assertInternalType('string', $result);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }

View File

@@ -312,10 +312,10 @@ class StringyTestCase extends CommonTest
/** /**
* @dataProvider slugifyProvider() * @dataProvider slugifyProvider()
*/ */
public function testSlugify($expected, $str) public function testSlugify($expected, $str, $replacement = '-')
{ {
$stringy = S::create($str); $stringy = S::create($str);
$result = $stringy->slugify(); $result = $stringy->slugify($replacement);
$this->assertInstanceOf('Stringy\Stringy', $result); $this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy); $this->assertEquals($str, $stringy);