1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Defines \TraderInteractive\Util\Strings class. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace TraderInteractive\Util; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Static class with various string functions. |
10
|
|
|
*/ |
11
|
|
|
final class Strings |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Replaces the format items in a specified string with the string representation of n specified objects. |
15
|
|
|
* |
16
|
|
|
* @param string $format A composit format string |
17
|
|
|
* @param mixed $arguments Variable number of items to format. |
18
|
|
|
* |
19
|
|
|
* @return string Returns a copy of format in which the format items have been |
20
|
|
|
* replaced by the string representations of arg0, arg1,... argN. |
21
|
|
|
*/ |
22
|
|
|
public static function format(string $format, string ...$arguments) : string |
23
|
|
|
{ |
24
|
|
|
foreach ($arguments as $key => $value) { |
25
|
|
|
$format = str_replace("{{$key}}", (string)$value, $format); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $format; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Checks if $string ends with $suffix and puts the rest of the $string in $nonSuffix. |
33
|
|
|
* |
34
|
|
|
* @param string $string The string to check |
35
|
|
|
* @param string $suffix The suffix to check for |
36
|
|
|
* @param mixed &$nonSuffix This is the part of the string that is not the suffix. |
37
|
|
|
* |
38
|
|
|
* @return bool whether the $string ended with $suffix or not. |
39
|
|
|
*/ |
40
|
|
|
public static function endsWith(string $string, string $suffix, &$nonSuffix = null) : bool |
41
|
|
|
{ |
42
|
|
|
$suffixLength = strlen($suffix); |
43
|
|
|
|
44
|
|
|
if ($suffixLength === 0) { |
45
|
|
|
$nonSuffix = $string; |
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (empty($string)) { |
50
|
|
|
$nonSuffix = ''; |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (substr_compare($string, $suffix, -$suffixLength, $suffixLength) !== 0) { |
55
|
|
|
$nonSuffix = $string; |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$nonSuffix = substr($string, 0, -$suffixLength); |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Truncates the string to the given length, with an ellipsis at the end. |
65
|
|
|
* |
66
|
|
|
* @param string $string The string to shorten. |
67
|
|
|
* @param int $maxLength The length to truncate the string to. The result will not be longer than this, but may be |
68
|
|
|
* shorter. |
69
|
|
|
* @param string $suffix The string to append when truncating. Typically this will be an ellipsis. |
70
|
|
|
* |
71
|
|
|
* @return string The truncated string with the ellipsis included if truncation occured. |
72
|
|
|
* |
73
|
|
|
* @throws \InvalidArgumentException if $maxLength is negative |
74
|
|
|
*/ |
75
|
|
|
public static function ellipsize(string $string, int $maxLength, string $suffix = '...') : string |
76
|
|
|
{ |
77
|
|
|
if ($maxLength < 0) { |
78
|
|
|
throw new \InvalidArgumentException('$maxLength is negative'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (strlen($string) <= $maxLength) { |
82
|
|
|
return $string; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$trimmedLength = $maxLength - strlen($suffix); |
86
|
|
|
$string = substr($string, 0, max(0, $trimmedLength)); |
87
|
|
|
|
88
|
|
|
if ($string === '') { |
89
|
|
|
return substr($suffix, 0, $maxLength); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $string . $suffix; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Uppercases words using custom word delimiters. |
97
|
|
|
* |
98
|
|
|
* This is more flexible than normal php ucwords because that only treats space as a word delimiter. |
99
|
|
|
* |
100
|
|
|
* Here is an example: |
101
|
|
|
* <code> |
102
|
|
|
* <?php |
103
|
|
|
* $string = 'break-down o\'boy up_town you+me here now,this:place'; |
104
|
|
|
* |
105
|
|
|
* echo String::ucwords($string); |
106
|
|
|
* // Break-Down O\'Boy Up_Town You+Me Here Now,This:Place |
107
|
|
|
* |
108
|
|
|
* echo String::ucwords($string, '- '); |
109
|
|
|
* // Break-Down O\'boy Up_town You+me Here Now,this:place |
110
|
|
|
* ?> |
111
|
|
|
* </code> |
112
|
|
|
* |
113
|
|
|
* @param string $string The string to titleize. |
114
|
|
|
* @param string $delimiters The characters to treat as word delimiters. |
115
|
|
|
* |
116
|
|
|
* @return string The titleized string. |
117
|
|
|
*/ |
118
|
|
|
public static function ucwords(string $string, string $delimiters = "-_+' \n\t\r\0\x0B:/,.") : string |
119
|
|
|
{ |
120
|
|
|
if ($delimiters === '') { |
121
|
|
|
return $string; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return preg_replace_callback( |
125
|
|
|
'/[^' . preg_quote($delimiters, '/') . ']+/', |
126
|
|
|
function ($matches) { |
127
|
|
|
return ucfirst($matches[0]); |
128
|
|
|
}, |
129
|
|
|
$string |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|