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 Trim |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Removes whitespace from the start and end of the string. |
20
|
|
|
* |
21
|
|
|
* Supports the removal of unicode whitespace. |
22
|
|
|
* Accepts an optional string of characters to |
23
|
|
|
* strip instead of the defaults. |
24
|
|
|
* |
25
|
|
|
* @param string|null $chars Optional string of characters to strip. |
26
|
|
|
* |
27
|
|
|
* @return static |
28
|
|
|
*/ |
29
|
|
|
public function trim($chars = null) |
30
|
|
|
{ |
31
|
|
|
$chars = $this->getTrimChars($chars); |
32
|
|
|
|
33
|
|
|
return $this->regexReplace("^[$chars]+|[$chars]+\$", ''); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns a string with whitespace removed from the start of the string. |
38
|
|
|
* Supports the removal of unicode whitespace. Accepts an optional |
39
|
|
|
* string of characters to strip instead of the defaults. |
40
|
|
|
* |
41
|
|
|
* @param string|null $chars Optional string of characters to strip. |
42
|
|
|
* |
43
|
|
|
* @return static |
44
|
|
|
*/ |
45
|
|
|
public function trimLeft($chars = null) |
46
|
|
|
{ |
47
|
|
|
return $this->regexReplace("^[".$this->getTrimChars($chars)."]+", ''); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns a string with whitespace removed from the end of the string. |
52
|
|
|
* Supports the removal of unicode whitespace. Accepts an optional |
53
|
|
|
* string of characters to strip instead of the defaults. |
54
|
|
|
* |
55
|
|
|
* @param string|null $chars Optional string of characters to strip. |
56
|
|
|
* |
57
|
|
|
* @return static |
58
|
|
|
*/ |
59
|
|
|
public function trimRight($chars = null) |
60
|
|
|
{ |
61
|
|
|
return $this->regexReplace("[".$this->getTrimChars($chars)."]+\$", ''); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Internal helper method for trim methods. |
66
|
|
|
* |
67
|
|
|
* @param string $chars |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
protected function getTrimChars($chars) |
71
|
|
|
{ |
72
|
|
|
if (!$chars) |
73
|
|
|
{ |
74
|
|
|
return '[:space:]'; |
75
|
|
|
} |
76
|
|
|
else |
77
|
|
|
{ |
78
|
|
|
return preg_quote($chars, $this->regexDelimiter); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|