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

Add support to slugify

This commit is contained in:
Daniel St. Jules
2017-06-11 20:41:02 -04:00
parent d9cee68330
commit 8ef4bddd15
2 changed files with 8 additions and 5 deletions

View File

@@ -810,13 +810,14 @@ random order.
s('fòôbàř')->shuffle(); // 'àôřbòf' s('fòôbàř')->shuffle(); // 'àôřbòf'
``` ```
##### slugify([, string $replacement = '-' ]) ##### slugify([, string $replacement = '-' [, string $language = 'en']])
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 remaining characters with their closest ASCII equivalents, removing remaining
non-ASCII and non-alphanumeric characters, and replacing whitespace with non-ASCII and non-alphanumeric characters, and replacing whitespace with
$replacement. The replacement defaults to a single dash, and the string $replacement. The replacement defaults to a single dash, and the string
is also converted to lowercase. is also converted to lowercase. The language of the source string can
also be supplied for language-specific transliteration.
```php ```php
s('Using strings like fòô bàř')->slugify(); // 'using-strings-like-foo-bar' s('Using strings like fòô bàř')->slugify(); // 'using-strings-like-foo-bar'

View File

@@ -1157,14 +1157,16 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* characters with their closest ASCII equivalents, removing remaining * characters with their closest ASCII equivalents, removing remaining
* non-ASCII and non-alphanumeric characters, and replacing whitespace with * non-ASCII and non-alphanumeric characters, and replacing whitespace with
* $replacement. The replacement defaults to a single dash, and the string * $replacement. The replacement defaults to a single dash, and the string
* is also converted to lowercase. * is also converted to lowercase. The language of the source string can
* also be supplied for language-specific transliteration.
* *
* @param string $replacement The string used to replace whitespace * @param string $replacement The string used to replace whitespace
* @param string $language Language of the source string
* @return static Object whose $str has been converted to an URL slug * @return static Object whose $str has been converted to an URL slug
*/ */
public function slugify($replacement = '-') public function slugify($replacement = '-', $language = 'en')
{ {
$stringy = $this->toAscii(); $stringy = $this->toAscii($language);
$stringy->str = str_replace('@', $replacement, $stringy); $stringy->str = str_replace('@', $replacement, $stringy);
$quotedReplacement = preg_quote($replacement); $quotedReplacement = preg_quote($replacement);