diff --git a/README.md b/README.md index 5e80a487..11a3d03b 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,14 @@ $slugify = new Slugify(['separator' => '_']); $slugify->slugify('Hello World'); // -> "hello_world" ``` +By default Slugify will remove leading and trailing separators before returning the slug. If you do not want the slug to +be trimmed you can set the `trim` option to false. + +```php +$slugify = new Slugify(['trim' => false]); +$slugify->slugify('Hello World '); // -> "hello-world-" +``` + ### Changing options on the fly You can overwrite any of the above options on the fly by passing an options array as second argument to the `slugify()` diff --git a/src/Bridge/Symfony/Configuration.php b/src/Bridge/Symfony/Configuration.php index 53d20b64..d2f30d14 100644 --- a/src/Bridge/Symfony/Configuration.php +++ b/src/Bridge/Symfony/Configuration.php @@ -27,6 +27,7 @@ public function getConfigTreeBuilder() $rootNode ->children() ->booleanNode('lowercase')->end() + ->booleanNode('trim')->end() ->scalarNode('separator')->end() ->scalarNode('regexp')->end() ->arrayNode('rulesets')->prototype('scalar')->end() diff --git a/src/Slugify.php b/src/Slugify.php index 501fd4a3..9e059d67 100644 --- a/src/Slugify.php +++ b/src/Slugify.php @@ -45,6 +45,7 @@ class Slugify implements SlugifyInterface 'regexp' => self::LOWERCASE_NUMBERS_DASHES, 'separator' => '-', 'lowercase' => true, + 'trim' => true, 'rulesets' => [ 'default', // Languages are preferred if they appear later, list is ordered by number of @@ -119,7 +120,9 @@ public function slugify($string, $options = null) $string = preg_replace($options['regexp'], $options['separator'], $string); - return trim($string, $options['separator']); + return ($options['trim']) + ? trim($string, $options['separator']) + : $string; } /** diff --git a/tests/SlugifyTest.php b/tests/SlugifyTest.php index 7a1a223a..cc94bfa1 100644 --- a/tests/SlugifyTest.php +++ b/tests/SlugifyTest.php @@ -207,6 +207,9 @@ public function slugifyOptionsArray() $this->assertEquals('file-name', $this->slugify->slugify('FILE NAME')); $this->assertEquals('FILE-NAME', $this->slugify->slugify('FILE NAME', ['lowercase' => false])); + + $this->assertEquals('file-name', $this->slugify->slugify('file name ')); + $this->assertEquals('file-name-', $this->slugify->slugify('file name ', ['trim' => false])); } /**