|
1
|
|
|
<?php namespace Gears\String\Methods; |
|
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
3
|
|
|
// __________ __ ________ __________ |
|
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
|
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
|
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
|
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
|
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
|
9
|
|
|
// ----------------------------------------------------------------------------- |
|
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
|
11
|
|
|
// ----------------------------------------------------------------------------- |
|
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
13
|
|
|
|
|
14
|
|
|
use voku\helper\UTF8; |
|
15
|
|
|
|
|
16
|
|
|
trait Replace |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Replaces all occurrences of $search in $str by $replacement. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string|array $search The needle to search for. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string|array $replacement The string to replace with. |
|
24
|
|
|
* |
|
25
|
|
|
* @param bool $caseSensitive To enforce case-sensitivity or not. |
|
26
|
|
|
* |
|
27
|
|
|
* @return static String after the replacements. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function replace($search, $replacement, $caseSensitive = true) |
|
30
|
|
|
{ |
|
31
|
|
|
if ($caseSensitive) |
|
32
|
|
|
{ |
|
33
|
|
|
$return = UTF8::str_replace |
|
34
|
|
|
( |
|
35
|
|
|
$search, |
|
36
|
|
|
$replacement, |
|
37
|
|
|
$this->scalarString |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
else |
|
41
|
|
|
{ |
|
42
|
|
|
$return = UTF8::str_ireplace |
|
43
|
|
|
( |
|
44
|
|
|
$search, |
|
45
|
|
|
$replacement, |
|
46
|
|
|
$this->scalarString |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->newSelf($return); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Replaces all occurrences of $search from the |
|
55
|
|
|
* beginning of string with $replacement. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $search |
|
58
|
|
|
* @param string $replacement |
|
59
|
|
|
* @return static |
|
60
|
|
|
*/ |
|
61
|
|
|
public function replaceBeginning($search, $replacement) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->regexReplace |
|
64
|
|
|
( |
|
65
|
|
|
'^'.preg_quote($search, '/'), |
|
66
|
|
|
UTF8::str_replace('\\', '\\\\', $replacement) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Replaces all occurrences of $search from the |
|
72
|
|
|
* ending of string with $replacement. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $search |
|
75
|
|
|
* @param string $replacement |
|
76
|
|
|
* @return static |
|
77
|
|
|
*/ |
|
78
|
|
|
public function replaceEnding($search, $replacement) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->regexReplace |
|
81
|
|
|
( |
|
82
|
|
|
preg_quote($search, '/').'$', |
|
83
|
|
|
UTF8::str_replace('\\', '\\\\', $replacement) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|