mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-09-01 17:12:42 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
6ba29637b2 | ||
|
2ada00ff62 | ||
|
2a197244a5 | ||
|
8b1a91fe0c | ||
|
906f217dc1 | ||
|
d4baab7583 | ||
|
0c8f0e9083 |
@@ -1,3 +1,8 @@
|
||||
### 1.2.2 (2013-12-04)
|
||||
|
||||
* Updated create function to use late static binding
|
||||
* Added optional $replacement param to slugify
|
||||
|
||||
### 1.2.1 (2013-10-11)
|
||||
|
||||
* Cleaned up tests
|
||||
|
11
README.md
11
README.md
@@ -1,4 +1,4 @@
|
||||
# Stringy
|
||||

|
||||
|
||||
A PHP library with a variety of string manipulation functions with multibyte support. Offers both OO method chaining and a procedural-style static wrapper. Compatible with PHP 5.3+. Inspired by underscore.string.js.
|
||||
|
||||
@@ -648,14 +648,15 @@ S::shuffle('fòô bàř', 'UTF-8'); // 'àôřb òf'
|
||||
|
||||
#### slugify
|
||||
|
||||
$stringy->slugify()
|
||||
$stringy->slugify([ string $replacement = '-' ])
|
||||
|
||||
S::slugify(string $str)
|
||||
S::slugify(string $str [, string $replacement = '-' ])
|
||||
|
||||
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.
|
||||
and non-ASCII characters, and replacing whitespace with $replacement.
|
||||
The replacement defaults to a single dash, and the string is also
|
||||
converted to lowercase.
|
||||
|
||||
```php
|
||||
S::create('Using strings like fòô bàř')->slugify();
|
||||
|
@@ -325,15 +325,17 @@ 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.
|
||||
* and non-ASCII characters, and replacing whitespace with $replacement.
|
||||
* The replacement defaults to a single dash, and the string is also
|
||||
* converted to lowercase.
|
||||
*
|
||||
* @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 used to replace whitespace
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -33,7 +33,7 @@ class Stringy
|
||||
*/
|
||||
public static function create($str, $encoding = null)
|
||||
{
|
||||
return new self($str, $encoding);
|
||||
return new static($str, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -577,18 +577,20 @@ 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.
|
||||
* and non-ASCII characters, and replacing whitespace with $replacement.
|
||||
* The replacement defaults to a single dash, and the string is also
|
||||
* converted to lowercase.
|
||||
*
|
||||
* @param string $replacement The string used to replace whitespace
|
||||
* @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;
|
||||
}
|
||||
|
@@ -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', '_')
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user