From f74f535b7802965d284395d79f1c7775bd12ee50 Mon Sep 17 00:00:00 2001 From: Yuri Druzhkov Date: Tue, 15 Apr 2014 10:09:11 +0400 Subject: [PATCH] add preg_quote, add test on regex special symbols --- src/Stringy.php | 7 +++---- tests/CommonTest.php | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Stringy.php b/src/Stringy.php index 7cc3649..b215c32 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -714,13 +714,12 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess { $stringy = self::create($this->str, $this->encoding); - $stringy->str = preg_replace("/[^a-zA-Z\d\- $replacement]/u", '', + $stringy->str = preg_replace("/[^a-zA-Z\d\s-" . preg_quote($replacement) . "]/u", '', $stringy->toAscii()); - $stringy->str = preg_replace('/\-/u', ' ', $stringy->str); $stringy->str = $stringy->collapseWhitespace()->str; - $stringy->str = str_replace(' ', $replacement, strtolower($stringy)); - $stringy->str = preg_replace("/[$replacement]+/u", $replacement, $stringy); + $stringy->str = preg_replace('/[-\s]/u', $replacement, strtolower($stringy)); + $stringy->str = preg_replace("/[" . preg_quote($replacement) . "]+/u", $replacement, $stringy); return $stringy; } diff --git a/tests/CommonTest.php b/tests/CommonTest.php index aa4e620..bb223e2 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -346,7 +346,8 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase 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', '\\') ); }