mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-13 16:53:59 +02:00
Add slugify
This commit is contained in:
21
README.md
21
README.md
@@ -275,7 +275,7 @@ Converts each tab in a string to some number of spaces, as defined by
|
|||||||
$tabLength. By default, each tab is converted to 4 consecutive spaces.
|
$tabLength. By default, each tab is converted to 4 consecutive spaces.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
S:::toSpaces(' String speech = "Hi"') \\ ' String speech = "Hi"'
|
S::toSpaces(' String speech = "Hi"') \\ ' String speech = "Hi"'
|
||||||
```
|
```
|
||||||
|
|
||||||
##### toTabs
|
##### toTabs
|
||||||
@@ -287,15 +287,24 @@ by $tabLength, to a tab. By default, each 4 consecutive spaces are
|
|||||||
converted to a tab.
|
converted to a tab.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
S:::toTabs(" fòô bàř") \\ " fòô bàř"
|
S::toTabs(" fòô bàř") \\ " fòô bàř"
|
||||||
|
```
|
||||||
|
|
||||||
|
##### slugify
|
||||||
|
|
||||||
|
S::slugify(string $str)
|
||||||
|
|
||||||
|
Converts the supplied text 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.
|
||||||
|
|
||||||
|
```php
|
||||||
|
S::slugify('Using strings like fòô bàř') // 'using-strings-like-foo-bar'
|
||||||
```
|
```
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
**toAnchor**
|
|
||||||
|
|
||||||
**slugify**
|
|
||||||
|
|
||||||
**contains**
|
**contains**
|
||||||
|
|
||||||
**between**
|
**between**
|
||||||
|
@@ -450,6 +450,22 @@ class Stringy {
|
|||||||
return str_replace($spaces, "\t", $str);
|
return str_replace($spaces, "\t", $str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the supplied text 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.
|
||||||
|
*
|
||||||
|
* @param string $str Text to transform into an URL slug
|
||||||
|
* @return string The corresponding URL slug
|
||||||
|
*/
|
||||||
|
public static function slugify($str) {
|
||||||
|
$str = preg_replace('/[^a-zA-Z\d -]/u', '', self::standardize($str));
|
||||||
|
$str = self::clean($str);
|
||||||
|
|
||||||
|
return self::dasherize(strtolower($str));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@@ -485,6 +485,27 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
|
|||||||
return $testData;
|
return $testData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider stringsForSlugify
|
||||||
|
*/
|
||||||
|
public function testSlugify($expected, $string) {
|
||||||
|
$result = S::slugify($string);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stringsForSlugify() {
|
||||||
|
$testData = array(
|
||||||
|
array('foo-bar', ' foo bar '),
|
||||||
|
array('foo-dbar', " Foo d'Bar "),
|
||||||
|
array('a-string-with-dashes', 'A string-with-dashes'),
|
||||||
|
array('using-strings-like-foo-bar', 'Using strings like fòô bàř'),
|
||||||
|
array('unrecognized-chars-like', 'unrecognized chars like συγγρ'),
|
||||||
|
array('numbers-1234', 'numbers 1234')
|
||||||
|
);
|
||||||
|
|
||||||
|
return $testData;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
Reference in New Issue
Block a user