mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-14 17:24:29 +02:00
Update replace() to correctly apply preg_quote to pattern and replacement
This commit is contained in:
@@ -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.
|
in the bundled version of Oniguruma in PHP 5.3.
|
||||||
|
|
||||||
```php
|
```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àř'
|
S::regexReplace('fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'); // 'bàř'
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -593,11 +593,11 @@ S::removeRight('fòô bàř', ' bàř', 'UTF-8'); // 'fòô'
|
|||||||
|
|
||||||
#### replace
|
#### 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
|
```php
|
||||||
S::create('fòô bàř fòô bàř', 'UTF-8')->replace('fòô ', '');
|
S::create('fòô bàř fòô bàř', 'UTF-8')->replace('fòô ', '');
|
||||||
|
@@ -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 $str The haystack to search through
|
||||||
* @param string $search The needle to search for
|
* @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
|
* @param string $encoding The character encoding
|
||||||
* @return string The resulting string after the replacements
|
* @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -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 $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
|
* @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();
|
$regexEncoding = mb_regex_encoding();
|
||||||
mb_regex_encoding($stringy->encoding);
|
mb_regex_encoding($this->encoding);
|
||||||
|
|
||||||
// Don't want the args accidentally being parsed as regex
|
// Don't want the args being parsed as regex
|
||||||
$quotedSearch = preg_quote($search);
|
$search = preg_quote($search);
|
||||||
$quotedReplace = preg_quote($replace);
|
$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);
|
mb_regex_encoding($regexEncoding);
|
||||||
|
|
||||||
return $stringy;
|
return self::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -925,12 +925,14 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
$testData = array(
|
$testData = array(
|
||||||
array('', '', '', ''),
|
array('', '', '', ''),
|
||||||
array('foo', '', '', 'foo'),
|
array('foo', '', '', 'foo'),
|
||||||
|
array('foo', '\s', '\s', 'foo'),
|
||||||
array('foo bar', 'foo bar', '', ''),
|
array('foo bar', 'foo bar', '', ''),
|
||||||
array('bar', 'foo bar', 'foo ', ''),
|
array('bar', 'foo bar', 'foo ', ''),
|
||||||
array('far bar', 'foo bar', 'foo', 'far'),
|
array('far bar', 'foo bar', 'foo', 'far'),
|
||||||
array('bar bar', 'foo bar foo bar', 'foo ', ''),
|
array('bar bar', 'foo bar foo bar', 'foo ', ''),
|
||||||
array('', '', '', '', 'UTF-8'),
|
array('', '', '', '', 'UTF-8'),
|
||||||
array('fòô', '', '', 'fòô', 'UTF-8'),
|
array('fòô', '', '', 'fòô', 'UTF-8'),
|
||||||
|
array('fòô', '\s', '\s', 'fòô', 'UTF-8'),
|
||||||
array('fòô bàř', 'fòô bàř', '', '', 'UTF-8'),
|
array('fòô bàř', 'fòô bàř', '', '', 'UTF-8'),
|
||||||
array('bàř', 'fòô bàř', 'fòô ', '', 'UTF-8'),
|
array('bàř', 'fòô bàř', 'fòô ', '', 'UTF-8'),
|
||||||
array('far bàř', 'fòô bàř', 'fòô', 'far', 'UTF-8'),
|
array('far bàř', 'fòô bàř', 'fòô', 'far', 'UTF-8'),
|
||||||
|
@@ -565,10 +565,10 @@ class StaticStringyTestCase extends CommonTest
|
|||||||
/**
|
/**
|
||||||
* @dataProvider stringsForReplace
|
* @dataProvider stringsForReplace
|
||||||
*/
|
*/
|
||||||
public function testReplace($expected, $str, $search, $replace,
|
public function testReplace($expected, $str, $search, $replacement,
|
||||||
$encoding = null)
|
$encoding = null)
|
||||||
{
|
{
|
||||||
$result = S::replace($str, $search, $replace, $encoding);
|
$result = S::replace($str, $search, $replacement, $encoding);
|
||||||
$this->assertInternalType('string', $result);
|
$this->assertInternalType('string', $result);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
@@ -666,11 +666,11 @@ class StringyTestCase extends CommonTest
|
|||||||
/**
|
/**
|
||||||
* @dataProvider stringsForReplace
|
* @dataProvider stringsForReplace
|
||||||
*/
|
*/
|
||||||
public function testReplace($expected, $str, $search, $replace,
|
public function testReplace($expected, $str, $search, $replacement,
|
||||||
$encoding = null)
|
$encoding = null)
|
||||||
{
|
{
|
||||||
$stringy = S::create($str, $encoding);
|
$stringy = S::create($str, $encoding);
|
||||||
$result = $stringy->replace($search, $replace);
|
$result = $stringy->replace($search, $replacement);
|
||||||
$this->assertInstanceOf('Stringy\Stringy', $result);
|
$this->assertInstanceOf('Stringy\Stringy', $result);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
$this->assertEquals($str, $stringy);
|
$this->assertEquals($str, $stringy);
|
||||||
|
Reference in New Issue
Block a user