Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trim option #189

Merged
merged 1 commit into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Symfony/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion src/Slugify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/SlugifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}

/**
Expand Down