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
This commit is contained in:
the_undefined 2008-05-14 15:35:36 +00:00
parent 830f9ec3f8
commit 7308c3e5d4
2 changed files with 28 additions and 1 deletions

View file

@ -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;
}

View file

@ -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() {