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

Remove leading or trailing replacement from slugified string, if present

This commit is contained in:
Daniel St. Jules
2014-04-19 00:33:59 -04:00
parent f74f535b78
commit ae7527ad4a
2 changed files with 7 additions and 10 deletions

View File

@@ -714,14 +714,12 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
{
$stringy = self::create($this->str, $this->encoding);
$stringy->str = preg_replace("/[^a-zA-Z\d\s-" . preg_quote($replacement) . "]/u", '',
$stringy->toAscii());
$quotedReplacement = preg_quote($replacement);
$pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u";
$stringy->str = preg_replace($pattern, '', $stringy->toAscii());
$stringy->str = $stringy->collapseWhitespace()->str;
$stringy->str = preg_replace('/[-\s]/u', $replacement, strtolower($stringy));
$stringy->str = preg_replace("/[" . preg_quote($replacement) . "]+/u", $replacement, $stringy);
return $stringy;
return $stringy->toLowerCase()->applyDelimeter($replacement)
->removeLeft($replacement)->removeRight($replacement);
}
/**

View File

@@ -341,13 +341,12 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('numbers-1234', 'numbers 1234'),
array('perevirka-ryadka', 'перевірка рядка'),
array('bukvar-s-bukvoy-y', 'букварь с буквой ы'),
// Conflicts between Russian or Bulgarian
//array('barzi-i-yarostni', 'бързи и яростни'),
array('podekhal-k-podezdu-moego-doma', 'подъехал к подъезду моего дома'),
array('foo:bar:baz', 'Foo bar baz', ':'),
array('a_string_with_underscores', 'A_string with_underscores', '_'),
array('a_string_with_dashes', 'A string-with-dashes', '_'),
array('a\string\with\dashes', 'A string-with-dashes', '\\')
array('a\string\with\dashes', 'A string-with-dashes', '\\'),
array('an_odd_string', '-- An odd__ string-_', '_')
);
}