forked from traderinteractive/util-string-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.php
More file actions
185 lines (162 loc) · 6 KB
/
Strings.php
File metadata and controls
185 lines (162 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Defines \DominionEnterprises\Util\Strings class.
*/
namespace DominionEnterprises\Util;
/**
* Static class with various string functions.
*/
final class Strings
{
/**
* Replaces the format items in a specified string with the string representation of n specified objects.
*
* @param string $format A composit format string
* @param mixed $arguments Variable number of items to format.
*
* @return string Returns a copy of format in which the format items have been
* replaced by the string representations of arg0, arg1,... argN.
*
* @throws \InvalidArgumentException Thrown if $format is not a string
* @throws \InvalidArgumentException Thrown if all arguments are not castable as strings or
* if less than two arguments are given
*/
public static function format($format, ...$arguments)
{
if (!is_string($format)) {
throw new \InvalidArgumentException('$format is not a string');
}
foreach ($arguments as $key => $value) {
if (is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
$format = str_replace("{{$key}}", (string)$value, $format);
continue;
}
throw new \InvalidArgumentException(
sprintf(
"Variable of type '%s' could not be converted to a string",
is_object($value) ? get_class($value) : gettype($value)
)
);
}
return $format;
}
/**
* Checks if $string ends with $suffix and puts the rest of the $string in $nonSuffix.
*
* @param string $string The string to check
* @param string $suffix The suffix to check for
* @param mixed &$nonSuffix This is the part of the string that is not the suffix.
*
* @return bool whether the $string ended with $suffix or not.
*
* @throws \InvalidArgumentException if $string is not a string
* @throws \InvalidArgumentException if $suffix is not a string
*/
public static function endsWith($string, $suffix, &$nonSuffix = null)
{
if (!is_string($string)) {
throw new \InvalidArgumentException('$string is not a string');
}
if (!is_string($suffix)) {
throw new \InvalidArgumentException('$suffix is not a string');
}
$suffixLength = strlen($suffix);
if ($suffixLength === 0) {
$nonSuffix = $string;
return true;
} elseif (empty($string)) {
$nonSuffix = '';
return false;
}
if (substr_compare($string, $suffix, -$suffixLength, $suffixLength) !== 0) {
$nonSuffix = $string;
return false;
}
$nonSuffix = substr($string, 0, -$suffixLength);
return true;
}
/**
* Truncates the string to the given length, with an ellipsis at the end.
*
* @param string $string The string to shorten.
* @param int $maxLength The length to truncate the string to. The result will not be longer than this, but may be
* shorter.
* @param string $suffix The string to append when truncating. Typically this will be an ellipsis.
*
* @return string The truncated string with the ellipsis included if truncation occured.
*
* @throws \InvalidArgumentException if $string is not a string
* @throws \InvalidArgumentException if $maxLength is not an integer
* @throws \InvalidArgumentException if $maxLength is negative
* @throws \InvalidArgumentException if $suffix is not a string
*/
public static function ellipsize($string, $maxLength, $suffix = '...')
{
if (!is_string($string)) {
throw new \InvalidArgumentException('$string is not a string');
}
if (!is_int($maxLength)) {
throw new \InvalidArgumentException('$maxLength is not an integer');
}
if ($maxLength < 0) {
throw new \InvalidArgumentException('$maxLength is negative');
}
if (!is_string($suffix)) {
throw new \InvalidArgumentException('$suffix is not a string');
}
if (strlen($string) <= $maxLength) {
return $string;
}
$trimmedLength = $maxLength - strlen($suffix);
$string = substr($string, 0, max(0, $trimmedLength));
if ($string === '') {
return substr($suffix, 0, $maxLength);
}
return $string . $suffix;
}
/**
* Uppercases words using custom word delimiters.
*
* This is more flexible than normal php ucwords because that only treats space as a word delimiter.
*
* Here is an example:
* <code>
* <?php
* $string = 'break-down o\'boy up_town you+me here now,this:place';
*
* echo String::ucwords($string);
* // Break-Down O\'Boy Up_Town You+Me Here Now,This:Place
*
* echo String::ucwords($string, '- ');
* // Break-Down O\'boy Up_town You+me Here Now,this:place
* ?>
* </code>
*
* @param string $string The string to titleize.
* @param string $delimiters The characters to treat as word delimiters.
*
* @return string The titleized string.
*
* @throws \InvalidArgumentException if $string is not a string
* @throws \InvalidArgumentException if $delimiters is not a string
*/
public static function ucwords($string, $delimiters = "-_+' \n\t\r\0\x0B:/,.")
{
if (!is_string($string)) {
throw new \InvalidArgumentException('$string is not a string');
}
if (!is_string($delimiters)) {
throw new \InvalidArgumentException('$delimiters is not a string');
}
if ($delimiters === '') {
return $string;
}
return preg_replace_callback(
'/[^' . preg_quote($delimiters, '/') . ']+/',
function ($matches) {
return ucfirst($matches[0]);
},
$string
);
}
}