From 7308c3e5d4933b143f8d27f3c7224c9a3dee3255 Mon Sep 17 00:00:00 2001 From: the_undefined Date: Wed, 14 May 2008 15:35:36 +0000 Subject: [PATCH] Fixed bug in String::insert Refactored and improved String::insert cleaning process git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6868 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/string.php | 19 ++++++++++++++++++- cake/tests/cases/libs/string.test.php | 10 ++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cake/libs/string.php b/cake/libs/string.php index f3e1da4b7..e3a7d003b 100644 --- a/cake/libs/string.php +++ b/cake/libs/string.php @@ -237,7 +237,24 @@ class String extends Object { $str = str_replace($options['escape'].$options['before'], $options['before'], $str); } if ($options['clean']) { - $str = preg_replace(sprintf('/(%s[^\s]+[\s]*|[\s]*%s[^\s]+)/', $options['before'], $options['before']), '', $str); + if ($options['clean'] === true) { + $options['clean'] = array(); + } + $options['clean'] = am(array( + 'word' => '[\w,]+', + 'gap' => '[\s]*(?:(?:and|or)[\s]*)?' + ), $options); + + $kleenex = sprintf( + '/(%s%s%s|%s%s%s)/', + $options['before'], + $options['clean']['word'], + $options['clean']['gap'], + $options['clean']['gap'], + $options['before'], + $options['clean']['word'] + ); + $str = preg_replace($kleenex, '', $str); } return $str; } diff --git a/cake/tests/cases/libs/string.test.php b/cake/tests/cases/libs/string.test.php index 5f6e7db85..ecdf5ee21 100644 --- a/cake/tests/cases/libs/string.test.php +++ b/cake/tests/cases/libs/string.test.php @@ -115,6 +115,16 @@ class StringTest extends UnitTestCase { $expected = '2 and 3'; $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true)); $this->assertEqual($result, $expected); + + $string = '":a, :b and :c"'; + $expected = '"1, 2"'; + $result = String::insert($string, array('a' => 1, 'b' => 2), array('clean' => true)); + $this->assertEqual($result, $expected); + + $string = '":a, :b and :c"'; + $expected = '"1, 2"'; + $result = String::insert($string, array('a' => 1, 'b' => 2), array('clean' => true)); + $this->assertEqual($result, $expected); } function testTokenize() {