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

Update replace() to correctly apply preg_quote to pattern and replacement

This commit is contained in:
Daniel St. Jules
2013-09-14 23:59:46 -04:00
parent 9bd5a9c0c8
commit 88145e83e8
6 changed files with 27 additions and 27 deletions

View File

@@ -561,7 +561,7 @@ in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
in the bundled version of Oniguruma in PHP 5.3.
```php
S::create('fòô ', UTF-8')->regexReplace('f[òô]+\s', 'bàř', 'msr');
S::create('fòô ', 'UTF-8')->regexReplace('f[òô]+\s', 'bàř', 'msr');
S::regexReplace('fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'); // 'bàř'
```
@@ -593,11 +593,11 @@ S::removeRight('fòô bàř', ' bàř', 'UTF-8'); // 'fòô'
#### replace
$stringy->replace(string $search, string $replace)
$stringy->replace(string $search, string $replacement)
S::replace(string $str, string $search, string $replace [, string $encoding ])
S::replace(string $str, string $search, string $replacement [, string $encoding ])
Returns a string with all occurrences of $search replaced with $replace.
Replaces all occurrences of $search in $str by $replacement.
```php
S::create('fòô bàř fòô bàř', 'UTF-8')->replace('fòô ', '');

View File

@@ -732,17 +732,17 @@ class StaticStringy
}
/**
* Returns a string with all occurrences of $search replaced with $replace.
* Replaces all occurrences of $search in $str by $replacement.
*
* @param string $str The haystack to search through
* @param string $search The needle to search for
* @param string $replace The string to replace with
* @param string $replacement The string to replace with
* @param string $encoding The character encoding
* @return string The resulting string after the replacements
*/
public static function replace($str, $search, $replace, $encoding = null)
public static function replace($str, $search, $replacement, $encoding = null)
{
return Stringy::create($str, $encoding)->replace($search, $replace)->str;
return Stringy::create($str, $encoding)->replace($search, $replacement)->str;
}
/**

View File

@@ -1124,27 +1124,25 @@ class Stringy
}
/**
* Returns a string with all occurrences of $search replaced with $replace.
* Replaces all occurrences of $search in $str by $replacement.
*
* @param string $search The needle to search for
* @param string $replace The string to replace with
* @param string $replacement The string to replace with
* @return Stringy Object with the resulting $str after the replacements
*/
public function replace($search, $replace)
public function replace($search, $replacement)
{
$stringy = self::create($this->str, $this->encoding);
$regexEncoding = mb_regex_encoding();
mb_regex_encoding($stringy->encoding);
mb_regex_encoding($this->encoding);
// Don't want the args accidentally being parsed as regex
$quotedSearch = preg_quote($search);
$quotedReplace = preg_quote($replace);
// Don't want the args being parsed as regex
$search = preg_quote($search);
$replacement = preg_quote($replacement);
$stringy->str = mb_ereg_replace($search, $replace, $stringy->str);
$str = mb_ereg_replace($search, $replacement, $this->str);
mb_regex_encoding($regexEncoding);
return $stringy;
return self::create($str, $this->encoding);
}
/**

View File

@@ -925,12 +925,14 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
$testData = array(
array('', '', '', ''),
array('foo', '', '', 'foo'),
array('foo', '\s', '\s', 'foo'),
array('foo bar', 'foo bar', '', ''),
array('bar', 'foo bar', 'foo ', ''),
array('far bar', 'foo bar', 'foo', 'far'),
array('bar bar', 'foo bar foo bar', 'foo ', ''),
array('', '', '', '', 'UTF-8'),
array('fòô', '', '', 'fòô', 'UTF-8'),
array('fòô', '\s', '\s', 'fòô', 'UTF-8'),
array('fòô bàř', 'fòô bàř', '', '', 'UTF-8'),
array('bàř', 'fòô bàř', 'fòô ', '', 'UTF-8'),
array('far bàř', 'fòô bàř', 'fòô', 'far', 'UTF-8'),

View File

@@ -565,10 +565,10 @@ class StaticStringyTestCase extends CommonTest
/**
* @dataProvider stringsForReplace
*/
public function testReplace($expected, $str, $search, $replace,
public function testReplace($expected, $str, $search, $replacement,
$encoding = null)
{
$result = S::replace($str, $search, $replace, $encoding);
$result = S::replace($str, $search, $replacement, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}

View File

@@ -666,11 +666,11 @@ class StringyTestCase extends CommonTest
/**
* @dataProvider stringsForReplace
*/
public function testReplace($expected, $str, $search, $replace,
public function testReplace($expected, $str, $search, $replacement,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->replace($search, $replace);
$result = $stringy->replace($search, $replacement);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);