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 Remove |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Returns a new string with the prefix $substring removed, if present. |
20
|
|
|
* |
21
|
|
|
* @param string $substring The prefix to remove. |
22
|
|
|
* |
23
|
|
|
* @return static String without the prefix $substring. |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function removeLeft($substring) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
if ($this->startsWith($substring)) |
28
|
|
|
{ |
29
|
|
|
return $this->newSelf |
30
|
|
|
( |
31
|
|
|
UTF8::substr |
32
|
|
|
( |
33
|
|
|
$this->scalarString, |
34
|
|
|
UTF8::strlen($substring, $this->encoding), |
35
|
|
|
null, |
36
|
|
|
$this->encoding |
37
|
|
|
) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns a new string with the suffix $substring removed, if present. |
46
|
|
|
* |
47
|
|
|
* @param string $substring The suffix to remove. |
48
|
|
|
* |
49
|
|
|
* @return static String without the suffix $substring. |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function removeRight($substring) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
if ($this->endsWith($substring)) |
54
|
|
|
{ |
55
|
|
|
return $this->newSelf |
56
|
|
|
( |
57
|
|
|
UTF8::substr |
58
|
|
|
( |
59
|
|
|
$this->scalarString, |
60
|
|
|
0, |
61
|
|
|
$this->getLength() - UTF8::strlen($substring, $this->encoding), |
62
|
|
|
$this->encoding |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Trims and replaces consecutive whitespace characters with a single space. |
72
|
|
|
* |
73
|
|
|
* This includes tabs and newline characters, as well as multibyte |
74
|
|
|
* whitespace such as the thin space and ideographic space. |
75
|
|
|
* |
76
|
|
|
* @return static Trimmed and condensed whitespace. |
77
|
|
|
*/ |
78
|
|
|
public function removeWhitespace() |
79
|
|
|
{ |
80
|
|
|
return $this->regexReplace('[[:space:]]+', ' ')->trim(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.