Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Stringy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Stringy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess |
||
15 | { |
||
16 | /** |
||
17 | * An instance's string. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $str; |
||
22 | |||
23 | /** |
||
24 | * The string's encoding, which should be one of the mbstring module's |
||
25 | * supported encodings. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $encoding; |
||
30 | |||
31 | /** |
||
32 | * Initializes a Stringy object and assigns both str and encoding properties |
||
33 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
34 | * $encoding is not specified, it defaults to mb_internal_encoding(). Throws |
||
35 | * an InvalidArgumentException if the first argument is an array or object |
||
36 | * without a __toString method. |
||
37 | * |
||
38 | * @param mixed $str Value to modify, after being cast to string |
||
39 | * @param string $encoding The character encoding |
||
40 | * |
||
41 | * @throws \InvalidArgumentException if an array or object without a |
||
42 | * __toString method is passed as the first argument |
||
43 | */ |
||
44 | 1020 | public function __construct($str = '', $encoding = null) |
|
45 | { |
||
46 | 1020 | if (is_array($str)) { |
|
47 | 1 | throw new \InvalidArgumentException( |
|
48 | 1 | 'Passed value cannot be an array' |
|
49 | ); |
||
50 | 1019 | } elseif (is_object($str) && !method_exists($str, '__toString')) { |
|
51 | 1 | throw new \InvalidArgumentException( |
|
52 | 1 | 'Passed object must have a __toString method' |
|
53 | ); |
||
54 | } |
||
55 | |||
56 | // don't throw a notice on PHP 5.3 |
||
57 | 1018 | if (!defined('ENT_SUBSTITUTE')) { |
|
58 | define('ENT_SUBSTITUTE', 8); |
||
59 | } |
||
60 | |||
61 | // init |
||
62 | 1018 | UTF8::checkForSupport(); |
|
63 | 1018 | $this->str = (string)$str; |
|
64 | |||
65 | 1018 | if ($encoding) { |
|
|
|||
66 | 806 | $this->encoding = $encoding; |
|
67 | } else { |
||
68 | 650 | UTF8::mbstring_loaded(); |
|
69 | 650 | $this->encoding = mb_internal_encoding(); |
|
70 | } |
||
71 | |||
72 | 1018 | if ($encoding) { |
|
73 | 806 | $this->encoding = $encoding; |
|
74 | } else { |
||
75 | 650 | $this->encoding = mb_internal_encoding(); |
|
76 | } |
||
77 | 1018 | } |
|
78 | |||
79 | /** |
||
80 | * Returns the value in $str. |
||
81 | * |
||
82 | * @return string The current value of the $str property |
||
83 | */ |
||
84 | 111 | public function __toString() |
|
88 | |||
89 | /** |
||
90 | * Returns a new string with $string appended. |
||
91 | * |
||
92 | * @param string $string The string to append |
||
93 | * |
||
94 | * @return Stringy Object with appended $string |
||
95 | */ |
||
96 | 5 | public function append($string) |
|
100 | |||
101 | /** |
||
102 | * Append an password (limited to chars that are good readable). |
||
103 | * |
||
104 | * @param int $length length of the random string |
||
105 | * |
||
106 | * @return Stringy Object with appended password |
||
107 | */ |
||
108 | 1 | public function appendPassword($length) |
|
114 | |||
115 | /** |
||
116 | * Append an unique identifier. |
||
117 | * |
||
118 | * @param string|int $extraPrefix |
||
119 | * |
||
120 | * @return Stringy Object with appended unique identifier as md5-hash |
||
121 | */ |
||
122 | 1 | public function appendUniqueIdentifier($extraPrefix = '') |
|
132 | |||
133 | /** |
||
134 | * Append an random string. |
||
135 | * |
||
136 | * @param int $length length of the random string |
||
137 | * @param string $possibleChars characters string for the random selection |
||
138 | * |
||
139 | * @return Stringy Object with appended random string |
||
140 | */ |
||
141 | 2 | public function appendRandomString($length, $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') |
|
142 | { |
||
143 | // init |
||
144 | 2 | $i = 0; |
|
145 | 2 | $length = (int)$length; |
|
146 | 2 | $str = $this->str; |
|
147 | 2 | $maxlength = UTF8::strlen($possibleChars, $this->encoding); |
|
148 | |||
149 | 2 | if ($maxlength === 0) { |
|
150 | 1 | return $this; |
|
151 | } |
||
152 | |||
153 | // add random chars |
||
154 | 2 | while ($i < $length) { |
|
155 | 2 | $char = UTF8::substr($possibleChars, mt_rand(0, $maxlength - 1), 1, $this->encoding); |
|
156 | 2 | $str .= $char; |
|
157 | 2 | $i++; |
|
158 | } |
||
159 | |||
160 | 2 | return $this->append($str); |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * Creates a Stringy object and assigns both str and encoding properties |
||
165 | * the supplied values. $str is cast to a string prior to assignment, and if |
||
166 | * $encoding is not specified, it defaults to mb_internal_encoding(). It |
||
167 | * then returns the initialized object. Throws an InvalidArgumentException |
||
168 | * if the first argument is an array or object without a __toString method. |
||
169 | * |
||
170 | * @param mixed $str Value to modify, after being cast to string |
||
171 | * @param string $encoding The character encoding |
||
172 | * |
||
173 | * @return Stringy A Stringy object |
||
174 | * @throws \InvalidArgumentException if an array or object without a |
||
175 | * __toString method is passed as the first argument |
||
176 | */ |
||
177 | 1010 | public static function create($str = '', $encoding = null) |
|
181 | |||
182 | /** |
||
183 | * Returns the substring between $start and $end, if found, or an empty |
||
184 | * string. An optional offset may be supplied from which to begin the |
||
185 | * search for the start string. |
||
186 | * |
||
187 | * @param string $start Delimiter marking the start of the substring |
||
188 | * @param string $end Delimiter marking the end of the substring |
||
189 | * @param int $offset Index from which to begin the search |
||
190 | * |
||
191 | * @return Stringy Object whose $str is a substring between $start and $end |
||
192 | */ |
||
193 | 16 | public function between($start, $end, $offset = 0) |
|
208 | |||
209 | /** |
||
210 | * Returns the index of the first occurrence of $needle in the string, |
||
211 | * and false if not found. Accepts an optional offset from which to begin |
||
212 | * the search. |
||
213 | * |
||
214 | * @param string $needle Substring to look for |
||
215 | * @param int $offset Offset from which to search |
||
216 | * |
||
217 | * @return int|bool The occurrence's index if found, otherwise false |
||
218 | */ |
||
219 | 28 | public function indexOf($needle, $offset = 0) |
|
223 | |||
224 | /** |
||
225 | * Returns the substring beginning at $start with the specified $length. |
||
226 | * It differs from the UTF8::substr() function in that providing a $length of |
||
227 | * null will return the rest of the string, rather than an empty string. |
||
228 | * |
||
229 | * @param int $start Position of the first character to use |
||
230 | * @param int $length Maximum number of characters used |
||
231 | * |
||
232 | * @return Stringy Object with its $str being the substring |
||
233 | */ |
||
234 | 64 | public function substr($start, $length = null) |
|
235 | { |
||
236 | 64 | if ($length === null) { |
|
237 | 19 | $length = $this->length(); |
|
238 | } |
||
239 | |||
240 | 64 | $str = UTF8::substr($this->str, $start, $length, $this->encoding); |
|
241 | |||
242 | 64 | return static::create($str, $this->encoding); |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * Returns the length of the string. |
||
247 | * |
||
248 | * @return int The number of characters in $str given the encoding |
||
249 | */ |
||
250 | 247 | public function length() |
|
254 | |||
255 | /** |
||
256 | * Trims the string and replaces consecutive whitespace characters with a |
||
257 | * single space. This includes tabs and newline characters, as well as |
||
258 | * multibyte whitespace such as the thin space and ideographic space. |
||
259 | * |
||
260 | * @return Stringy Object with a trimmed $str and condensed whitespace |
||
261 | */ |
||
262 | 13 | public function collapseWhitespace() |
|
266 | |||
267 | /** |
||
268 | * Returns a string with whitespace removed from the start and end of the |
||
269 | * string. Supports the removal of unicode whitespace. Accepts an optional |
||
270 | * string of characters to strip instead of the defaults. |
||
271 | * |
||
272 | * @param string $chars Optional string of characters to strip |
||
273 | * |
||
274 | * @return Stringy Object with a trimmed $str |
||
275 | */ |
||
276 | 114 | View Code Duplication | public function trim($chars = null) |
277 | { |
||
278 | 114 | if (!$chars) { |
|
279 | 113 | $chars = '[:space:]'; |
|
280 | } else { |
||
281 | 1 | $chars = preg_quote($chars, '/'); |
|
282 | } |
||
283 | |||
284 | 114 | return $this->regexReplace("^[$chars]+|[$chars]+\$", ''); |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
289 | * |
||
290 | * @param string $pattern The regular expression pattern |
||
291 | * @param string $replacement The string to replace with |
||
292 | * @param string $options Matching conditions to be used |
||
293 | * |
||
294 | * @return Stringy Object with the result2ing $str after the replacements |
||
295 | */ |
||
296 | 184 | public function regexReplace($pattern, $replacement, $options = '') |
|
297 | { |
||
298 | 184 | if ($options === 'msr') { |
|
299 | 8 | $options = 'ms'; |
|
300 | } |
||
301 | |||
302 | 184 | $str = preg_replace( |
|
303 | 184 | '/' . $pattern . '/u' . $options, |
|
304 | $replacement, |
||
305 | 184 | $this->str |
|
306 | ); |
||
307 | |||
308 | 184 | return static::create($str, $this->encoding); |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * Returns true if the string contains all $needles, false otherwise. By |
||
313 | * default the comparison is case-sensitive, but can be made insensitive by |
||
314 | * setting $caseSensitive to false. |
||
315 | * |
||
316 | * @param array $needles SubStrings to look for |
||
317 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
318 | * |
||
319 | * @return bool Whether or not $str contains $needle |
||
320 | */ |
||
321 | 43 | View Code Duplication | public function containsAll($needles, $caseSensitive = true) |
322 | { |
||
323 | /** @noinspection IsEmptyFunctionUsageInspection */ |
||
324 | 43 | if (empty($needles)) { |
|
325 | 1 | return false; |
|
326 | } |
||
327 | |||
328 | 42 | foreach ($needles as $needle) { |
|
329 | 42 | if (!$this->contains($needle, $caseSensitive)) { |
|
330 | 42 | return false; |
|
331 | } |
||
332 | } |
||
333 | |||
334 | 24 | return true; |
|
335 | } |
||
336 | |||
337 | /** |
||
338 | * Returns true if the string contains $needle, false otherwise. By default |
||
339 | * the comparison is case-sensitive, but can be made insensitive by setting |
||
340 | * $caseSensitive to false. |
||
341 | * |
||
342 | * @param string $needle Substring to look for |
||
343 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
344 | * |
||
345 | * @return bool Whether or not $str contains $needle |
||
346 | */ |
||
347 | 105 | public function contains($needle, $caseSensitive = true) |
|
357 | |||
358 | /** |
||
359 | * Returns true if the string contains any $needles, false otherwise. By |
||
360 | * default the comparison is case-sensitive, but can be made insensitive by |
||
361 | * setting $caseSensitive to false. |
||
362 | * |
||
363 | * @param array $needles SubStrings to look for |
||
364 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
365 | * |
||
366 | * @return bool Whether or not $str contains $needle |
||
367 | */ |
||
368 | 43 | View Code Duplication | public function containsAny($needles, $caseSensitive = true) |
369 | { |
||
370 | /** @noinspection IsEmptyFunctionUsageInspection */ |
||
371 | 43 | if (empty($needles)) { |
|
372 | 1 | return false; |
|
373 | } |
||
374 | |||
375 | 42 | foreach ($needles as $needle) { |
|
376 | 42 | if ($this->contains($needle, $caseSensitive)) { |
|
377 | 42 | return true; |
|
378 | } |
||
379 | } |
||
380 | |||
381 | 18 | return false; |
|
382 | } |
||
383 | |||
384 | /** |
||
385 | * Returns the length of the string, implementing the countable interface. |
||
386 | * |
||
387 | * @return int The number of characters in the string, given the encoding |
||
388 | */ |
||
389 | 1 | public function count() |
|
393 | |||
394 | /** |
||
395 | * Returns the number of occurrences of $substring in the given string. |
||
396 | * By default, the comparison is case-sensitive, but can be made insensitive |
||
397 | * by setting $caseSensitive to false. |
||
398 | * |
||
399 | * @param string $substring The substring to search for |
||
400 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
401 | * |
||
402 | * @return int The number of $substring occurrences |
||
403 | */ |
||
404 | 15 | public function countSubstr($substring, $caseSensitive = true) |
|
415 | |||
416 | /** |
||
417 | * Returns a lowercase and trimmed string separated by dashes. Dashes are |
||
418 | * inserted before uppercase characters (with the exception of the first |
||
419 | * character of the string), and in place of spaces as well as underscores. |
||
420 | * |
||
421 | * @return Stringy Object with a dasherized $str |
||
422 | */ |
||
423 | 19 | public function dasherize() |
|
427 | |||
428 | /** |
||
429 | * Returns a lowercase and trimmed string separated by the given delimiter. |
||
430 | * Delimiters are inserted before uppercase characters (with the exception |
||
431 | * of the first character of the string), and in place of spaces, dashes, |
||
432 | * and underscores. Alpha delimiters are not converted to lowercase. |
||
433 | * |
||
434 | * @param string $delimiter Sequence used to separate parts of the string |
||
435 | * |
||
436 | * @return Stringy Object with a delimited $str |
||
437 | */ |
||
438 | 49 | public function delimit($delimiter) |
|
450 | |||
451 | /** |
||
452 | * Ensures that the string begins with $substring. If it doesn't, it's |
||
453 | * prepended. |
||
454 | * |
||
455 | * @param string $substring The substring to add if not present |
||
456 | * |
||
457 | * @return Stringy Object with its $str prefixed by the $substring |
||
458 | */ |
||
459 | 10 | View Code Duplication | public function ensureLeft($substring) |
469 | |||
470 | /** |
||
471 | * Returns true if the string begins with $substring, false otherwise. By |
||
472 | * default, the comparison is case-sensitive, but can be made insensitive |
||
473 | * by setting $caseSensitive to false. |
||
474 | * |
||
475 | * @param string $substring The substring to look for |
||
476 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
477 | * |
||
478 | * @return bool Whether or not $str starts with $substring |
||
479 | */ |
||
480 | 33 | public function startsWith($substring, $caseSensitive = true) |
|
491 | |||
492 | /** |
||
493 | * Ensures that the string ends with $substring. If it doesn't, it's |
||
494 | * appended. |
||
495 | * |
||
496 | * @param string $substring The substring to add if not present |
||
497 | * |
||
498 | * @return Stringy Object with its $str suffixed by the $substring |
||
499 | */ |
||
500 | 10 | View Code Duplication | public function ensureRight($substring) |
510 | |||
511 | /** |
||
512 | * Returns true if the string ends with $substring, false otherwise. By |
||
513 | * default, the comparison is case-sensitive, but can be made insensitive |
||
514 | * by setting $caseSensitive to false. |
||
515 | * |
||
516 | * @param string $substring The substring to look for |
||
517 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
518 | * |
||
519 | * @return bool Whether or not $str ends with $substring |
||
520 | */ |
||
521 | 33 | public function endsWith($substring, $caseSensitive = true) |
|
540 | |||
541 | /** |
||
542 | * Returns the first $n characters of the string. |
||
543 | * |
||
544 | * @param int $n Number of characters to retrieve from the start |
||
545 | * |
||
546 | * @return Stringy Object with its $str being the first $n chars |
||
547 | */ |
||
548 | 12 | View Code Duplication | public function first($n) |
560 | |||
561 | /** |
||
562 | * Returns the encoding used by the Stringy object. |
||
563 | * |
||
564 | * @return string The current value of the $encoding property |
||
565 | */ |
||
566 | 3 | public function getEncoding() |
|
570 | |||
571 | /** |
||
572 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
573 | * interface. The ArrayIterator's constructor is passed an array of chars |
||
574 | * in the multibyte string. This enables the use of foreach with instances |
||
575 | * of Stringy\Stringy. |
||
576 | * |
||
577 | * @return \ArrayIterator An iterator for the characters in the string |
||
578 | */ |
||
579 | 1 | public function getIterator() |
|
583 | |||
584 | /** |
||
585 | * Returns an array consisting of the characters in the string. |
||
586 | * |
||
587 | * @return array An array of string chars |
||
588 | */ |
||
589 | 4 | public function chars() |
|
601 | |||
602 | /** |
||
603 | * Returns the character at $index, with indexes starting at 0. |
||
604 | * |
||
605 | * @param int $index Position of the character |
||
606 | * |
||
607 | * @return Stringy The character at $index |
||
608 | */ |
||
609 | 11 | public function at($index) |
|
613 | |||
614 | /** |
||
615 | * Returns true if the string contains a lower case char, false |
||
616 | * otherwise. |
||
617 | * |
||
618 | * @return bool Whether or not the string contains a lower case character. |
||
619 | */ |
||
620 | 12 | public function hasLowerCase() |
|
624 | |||
625 | /** |
||
626 | * Returns true if $str matches the supplied pattern, false otherwise. |
||
627 | * |
||
628 | * @param string $pattern Regex pattern to match against |
||
629 | * |
||
630 | * @return bool Whether or not $str matches the pattern |
||
631 | */ |
||
632 | 91 | private function matchesPattern($pattern) |
|
640 | |||
641 | /** |
||
642 | * Returns true if the string contains an upper case char, false |
||
643 | * otherwise. |
||
644 | * |
||
645 | * @return bool Whether or not the string contains an upper case character. |
||
646 | */ |
||
647 | 12 | public function hasUpperCase() |
|
651 | |||
652 | /** |
||
653 | * Convert all HTML entities to their applicable characters. |
||
654 | * |
||
655 | * @param int|null $flags Optional flags |
||
656 | * |
||
657 | * @return Stringy Object with the resulting $str after being html decoded. |
||
658 | */ |
||
659 | 5 | public function htmlDecode($flags = ENT_COMPAT) |
|
665 | |||
666 | /** |
||
667 | * Convert all applicable characters to HTML entities. |
||
668 | * |
||
669 | * @param int|null $flags Optional flags |
||
670 | * |
||
671 | * @return Stringy Object with the resulting $str after being html encoded. |
||
672 | */ |
||
673 | 5 | public function htmlEncode($flags = ENT_COMPAT) |
|
679 | |||
680 | /** |
||
681 | * Capitalizes the first word of the string, replaces underscores with |
||
682 | * spaces, and strips '_id'. |
||
683 | * |
||
684 | * @return Stringy Object with a humanized $str |
||
685 | */ |
||
686 | 3 | public function humanize() |
|
692 | |||
693 | /** |
||
694 | * Converts the first character of the supplied string to upper case. |
||
695 | * |
||
696 | * @return Stringy Object with the first character of $str being upper case |
||
697 | */ |
||
698 | 27 | View Code Duplication | public function upperCaseFirst() |
712 | |||
713 | /** |
||
714 | * Returns the index of the last occurrence of $needle in the string, |
||
715 | * and false if not found. Accepts an optional offset from which to begin |
||
716 | * the search. Offsets may be negative to count from the last character |
||
717 | * in the string. |
||
718 | * |
||
719 | * @param string $needle Substring to look for |
||
720 | * @param int $offset Offset from which to search |
||
721 | * |
||
722 | * @return int|bool The last occurrence's index if found, otherwise false |
||
723 | */ |
||
724 | 12 | public function indexOfLast($needle, $offset = 0) |
|
728 | |||
729 | /** |
||
730 | * Inserts $substring into the string at the $index provided. |
||
731 | * |
||
732 | * @param string $substring String to be inserted |
||
733 | * @param int $index The index at which to insert the substring |
||
734 | * |
||
735 | * @return Stringy Object with the resulting $str after the insertion |
||
736 | */ |
||
737 | 8 | View Code Duplication | public function insert($substring, $index) |
751 | |||
752 | /** |
||
753 | * Returns true if the string contains only alphabetic chars, false |
||
754 | * otherwise. |
||
755 | * |
||
756 | * @return bool Whether or not $str contains only alphabetic chars |
||
757 | */ |
||
758 | 10 | public function isAlpha() |
|
762 | |||
763 | /** |
||
764 | * Determine whether the string is considered to be empty. |
||
765 | * |
||
766 | * A variable is considered empty if it does not exist or if its value equals FALSE. |
||
767 | * empty() does not generate a warning if the variable does not exist. |
||
768 | * |
||
769 | * @return bool |
||
770 | */ |
||
771 | public function isEmpty() |
||
775 | |||
776 | /** |
||
777 | * Returns true if the string contains only alphabetic and numeric chars, |
||
778 | * false otherwise. |
||
779 | * |
||
780 | * @return bool Whether or not $str contains only alphanumeric chars |
||
781 | */ |
||
782 | 13 | public function isAlphanumeric() |
|
786 | |||
787 | /** |
||
788 | * Returns true if the string contains only whitespace chars, false |
||
789 | * otherwise. |
||
790 | * |
||
791 | * @return bool Whether or not $str contains only whitespace characters |
||
792 | */ |
||
793 | 15 | public function isBlank() |
|
797 | |||
798 | /** |
||
799 | * Returns true if the string contains only hexadecimal chars, false |
||
800 | * otherwise. |
||
801 | * |
||
802 | * @return bool Whether or not $str contains only hexadecimal chars |
||
803 | */ |
||
804 | 13 | public function isHexadecimal() |
|
808 | |||
809 | /** |
||
810 | * Returns true if the string contains HTML-Tags, false otherwise. |
||
811 | * |
||
812 | * @return bool Whether or not $str contains HTML-Tags |
||
813 | */ |
||
814 | 1 | public function isHtml() |
|
818 | |||
819 | /** |
||
820 | * Returns true if the string is JSON, false otherwise. Unlike json_decode |
||
821 | * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
||
822 | * in that an empty string is not considered valid JSON. |
||
823 | * |
||
824 | * @return bool Whether or not $str is JSON |
||
825 | */ |
||
826 | 20 | public function isJson() |
|
840 | |||
841 | /** |
||
842 | * Returns true if the string contains only lower case chars, false |
||
843 | * otherwise. |
||
844 | * |
||
845 | * @return bool Whether or not $str contains only lower case characters |
||
846 | */ |
||
847 | 8 | public function isLowerCase() |
|
855 | |||
856 | /** |
||
857 | * Returns true if the string is serialized, false otherwise. |
||
858 | * |
||
859 | * @return bool Whether or not $str is serialized |
||
860 | */ |
||
861 | 7 | public function isSerialized() |
|
878 | |||
879 | /** |
||
880 | * Returns true if the string contains only lower case chars, false |
||
881 | * otherwise. |
||
882 | * |
||
883 | * @return bool Whether or not $str contains only lower case characters |
||
884 | */ |
||
885 | 8 | public function isUpperCase() |
|
889 | |||
890 | /** |
||
891 | * Returns the last $n characters of the string. |
||
892 | * |
||
893 | * @param int $n Number of characters to retrieve from the end |
||
894 | * |
||
895 | * @return Stringy Object with its $str being the last $n chars |
||
896 | */ |
||
897 | 12 | View Code Duplication | public function last($n) |
909 | |||
910 | /** |
||
911 | * Splits on newlines and carriage returns, returning an array of Stringy |
||
912 | * objects corresponding to the lines in the string. |
||
913 | * |
||
914 | * @return Stringy[] An array of Stringy objects |
||
915 | */ |
||
916 | 15 | public function lines() |
|
927 | |||
928 | /** |
||
929 | * Returns the longest common prefix between the string and $otherStr. |
||
930 | * |
||
931 | * @param string $otherStr Second string for comparison |
||
932 | * |
||
933 | * @return Stringy Object with its $str being the longest common prefix |
||
934 | */ |
||
935 | 10 | public function longestCommonPrefix($otherStr) |
|
953 | |||
954 | /** |
||
955 | * Returns the longest common suffix between the string and $otherStr. |
||
956 | * |
||
957 | * @param string $otherStr Second string for comparison |
||
958 | * |
||
959 | * @return Stringy Object with its $str being the longest common suffix |
||
960 | */ |
||
961 | 10 | public function longestCommonSuffix($otherStr) |
|
979 | |||
980 | /** |
||
981 | * Returns the longest common substring between the string and $otherStr. |
||
982 | * In the case of ties, it returns that which occurs first. |
||
983 | * |
||
984 | * @param string $otherStr Second string for comparison |
||
985 | * |
||
986 | * @return Stringy Object with its $str being the longest common substring |
||
987 | */ |
||
988 | 10 | public function longestCommonSubstring($otherStr) |
|
1032 | |||
1033 | /** |
||
1034 | * Returns whether or not a character exists at an index. Offsets may be |
||
1035 | * negative to count from the last character in the string. Implements |
||
1036 | * part of the ArrayAccess interface. |
||
1037 | * |
||
1038 | * @param mixed $offset The index to check |
||
1039 | * |
||
1040 | * @return boolean Whether or not the index exists |
||
1041 | */ |
||
1042 | 6 | public function offsetExists($offset) |
|
1054 | |||
1055 | /** |
||
1056 | * Returns the character at the given index. Offsets may be negative to |
||
1057 | * count from the last character in the string. Implements part of the |
||
1058 | * ArrayAccess interface, and throws an OutOfBoundsException if the index |
||
1059 | * does not exist. |
||
1060 | * |
||
1061 | * @param mixed $offset The index from which to retrieve the char |
||
1062 | * |
||
1063 | * @return string The character at the specified index |
||
1064 | * @throws \OutOfBoundsException If the positive or negative offset does |
||
1065 | * not exist |
||
1066 | */ |
||
1067 | 2 | public function offsetGet($offset) |
|
1083 | |||
1084 | /** |
||
1085 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1086 | * when called. This maintains the immutability of Stringy objects. |
||
1087 | * |
||
1088 | * @param mixed $offset The index of the character |
||
1089 | * @param mixed $value Value to set |
||
1090 | * |
||
1091 | * @throws \Exception When called |
||
1092 | */ |
||
1093 | 1 | public function offsetSet($offset, $value) |
|
1098 | |||
1099 | /** |
||
1100 | * Implements part of the ArrayAccess interface, but throws an exception |
||
1101 | * when called. This maintains the immutability of Stringy objects. |
||
1102 | * |
||
1103 | * @param mixed $offset The index of the character |
||
1104 | * |
||
1105 | * @throws \Exception When called |
||
1106 | */ |
||
1107 | 1 | public function offsetUnset($offset) |
|
1112 | |||
1113 | /** |
||
1114 | * Pads the string to a given length with $padStr. If length is less than |
||
1115 | * or equal to the length of the string, no padding takes places. The |
||
1116 | * default string used for padding is a space, and the default type (one of |
||
1117 | * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
||
1118 | * if $padType isn't one of those 3 values. |
||
1119 | * |
||
1120 | * @param int $length Desired string length after padding |
||
1121 | * @param string $padStr String used to pad, defaults to space |
||
1122 | * @param string $padType One of 'left', 'right', 'both' |
||
1123 | * |
||
1124 | * @return Stringy Object with a padded $str |
||
1125 | * @throws \InvalidArgumentException If $padType isn't one of 'right', 'left' or 'both' |
||
1126 | */ |
||
1127 | 13 | public function pad($length, $padStr = ' ', $padType = 'right') |
|
1144 | |||
1145 | /** |
||
1146 | * Returns a new string of a given length such that the beginning of the |
||
1147 | * string is padded. Alias for pad() with a $padType of 'left'. |
||
1148 | * |
||
1149 | * @param int $length Desired string length after padding |
||
1150 | * @param string $padStr String used to pad, defaults to space |
||
1151 | * |
||
1152 | * @return Stringy String with left padding |
||
1153 | */ |
||
1154 | 10 | public function padLeft($length, $padStr = ' ') |
|
1158 | |||
1159 | /** |
||
1160 | * Adds the specified amount of left and right padding to the given string. |
||
1161 | * The default character used is a space. |
||
1162 | * |
||
1163 | * @param int $left Length of left padding |
||
1164 | * @param int $right Length of right padding |
||
1165 | * @param string $padStr String used to pad |
||
1166 | * |
||
1167 | * @return Stringy String with padding applied |
||
1168 | */ |
||
1169 | 37 | private function applyPadding($left = 0, $right = 0, $padStr = ' ') |
|
1206 | |||
1207 | /** |
||
1208 | * Returns a new string of a given length such that the end of the string |
||
1209 | * is padded. Alias for pad() with a $padType of 'right'. |
||
1210 | * |
||
1211 | * @param int $length Desired string length after padding |
||
1212 | * @param string $padStr String used to pad, defaults to space |
||
1213 | * |
||
1214 | * @return Stringy String with right padding |
||
1215 | */ |
||
1216 | 13 | public function padRight($length, $padStr = ' ') |
|
1220 | |||
1221 | /** |
||
1222 | * Returns a new string of a given length such that both sides of the |
||
1223 | * string are padded. Alias for pad() with a $padType of 'both'. |
||
1224 | * |
||
1225 | * @param int $length Desired string length after padding |
||
1226 | * @param string $padStr String used to pad, defaults to space |
||
1227 | * |
||
1228 | * @return Stringy String with padding applied |
||
1229 | */ |
||
1230 | 14 | public function padBoth($length, $padStr = ' ') |
|
1236 | |||
1237 | /** |
||
1238 | * Returns a new string starting with $string. |
||
1239 | * |
||
1240 | * @param string $string The string to append |
||
1241 | * |
||
1242 | * @return Stringy Object with appended $string |
||
1243 | */ |
||
1244 | 2 | public function prepend($string) |
|
1248 | |||
1249 | /** |
||
1250 | * Returns a new string with the prefix $substring removed, if present. |
||
1251 | * |
||
1252 | * @param string $substring The prefix to remove |
||
1253 | * |
||
1254 | * @return Stringy Object having a $str without the prefix $substring |
||
1255 | */ |
||
1256 | 12 | View Code Duplication | public function removeLeft($substring) |
1268 | |||
1269 | /** |
||
1270 | * Returns a new string with the suffix $substring removed, if present. |
||
1271 | * |
||
1272 | * @param string $substring The suffix to remove |
||
1273 | * |
||
1274 | * @return Stringy Object having a $str without the suffix $substring |
||
1275 | */ |
||
1276 | 12 | View Code Duplication | public function removeRight($substring) |
1288 | |||
1289 | /** |
||
1290 | * Returns a repeated string given a multiplier. |
||
1291 | * |
||
1292 | * @param int $multiplier The number of times to repeat the string |
||
1293 | * |
||
1294 | * @return Stringy Object with a repeated str |
||
1295 | */ |
||
1296 | 7 | public function repeat($multiplier) |
|
1302 | |||
1303 | /** |
||
1304 | * Replaces all occurrences of $search in $str by $replacement. |
||
1305 | * |
||
1306 | * @param string $search The needle to search for |
||
1307 | * @param string $replacement The string to replace with |
||
1308 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
1309 | * |
||
1310 | * @return Stringy Object with the resulting $str after the replacements |
||
1311 | */ |
||
1312 | 28 | View Code Duplication | public function replace($search, $replacement, $caseSensitive = true) |
1322 | |||
1323 | /** |
||
1324 | * Replaces all occurrences of $search in $str by $replacement. |
||
1325 | * |
||
1326 | * @param array $search The elements to search for |
||
1327 | * @param string|array $replacement The string to replace with |
||
1328 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
1329 | * |
||
1330 | * @return Stringy Object with the resulting $str after the replacements |
||
1331 | */ |
||
1332 | 30 | View Code Duplication | public function replaceAll(array $search, $replacement, $caseSensitive = true) |
1342 | |||
1343 | /** |
||
1344 | * Replaces all occurrences of $search from the beginning of string with $replacement |
||
1345 | * |
||
1346 | * @param string $search |
||
1347 | * @param string $replacement |
||
1348 | * |
||
1349 | * @return Stringy Object with the resulting $str after the replacements |
||
1350 | */ |
||
1351 | 16 | public function replaceBeginning($search, $replacement) |
|
1357 | |||
1358 | /** |
||
1359 | * Replaces all occurrences of $search from the ending of string with $replacement |
||
1360 | * |
||
1361 | * @param string $search |
||
1362 | * @param string $replacement |
||
1363 | * |
||
1364 | * @return Stringy Object with the resulting $str after the replacements |
||
1365 | */ |
||
1366 | 16 | public function replaceEnding($search, $replacement) |
|
1372 | |||
1373 | /** |
||
1374 | * Returns a reversed string. A multibyte version of strrev(). |
||
1375 | * |
||
1376 | * @return Stringy Object with a reversed $str |
||
1377 | */ |
||
1378 | 5 | public function reverse() |
|
1384 | |||
1385 | /** |
||
1386 | * Truncates the string to a given length, while ensuring that it does not |
||
1387 | * split words. If $substring is provided, and truncating occurs, the |
||
1388 | * string is further truncated so that the substring may be appended without |
||
1389 | * exceeding the desired length. |
||
1390 | * |
||
1391 | * @param int $length Desired length of the truncated string |
||
1392 | * @param string $substring The substring to append if it can fit |
||
1393 | * |
||
1394 | * @return Stringy Object with the resulting $str after truncating |
||
1395 | */ |
||
1396 | 22 | public function safeTruncate($length, $substring = '') |
|
1421 | |||
1422 | /** |
||
1423 | * A multibyte string shuffle function. It returns a string with its |
||
1424 | * characters in random order. |
||
1425 | * |
||
1426 | * @return Stringy Object with a shuffled $str |
||
1427 | */ |
||
1428 | 3 | public function shuffle() |
|
1434 | |||
1435 | /** |
||
1436 | * Converts the string into an URL slug. This includes replacing non-ASCII |
||
1437 | * characters with their closest ASCII equivalents, removing remaining |
||
1438 | * non-ASCII and non-alphanumeric characters, and replacing whitespace with |
||
1439 | * $replacement. The replacement defaults to a single dash, and the string |
||
1440 | * is also converted to lowercase. |
||
1441 | * |
||
1442 | * @param string $replacement The string used to replace whitespace |
||
1443 | * @param string $language The language for the url |
||
1444 | * @param bool $strToLower string to lower |
||
1445 | * |
||
1446 | * @return Stringy Object whose $str has been converted to an URL slug |
||
1447 | */ |
||
1448 | 15 | public function slugify($replacement = '-', $language = 'de', $strToLower = true) |
|
1454 | |||
1455 | /** |
||
1456 | * Remove css media-queries. |
||
1457 | * |
||
1458 | * @return Stringy |
||
1459 | */ |
||
1460 | 1 | public function stripeCssMediaQueries() |
|
1466 | |||
1467 | /** |
||
1468 | * Remove empty html-tag. |
||
1469 | * |
||
1470 | * e.g.: <tag></tag> |
||
1471 | * |
||
1472 | * @return Stringy |
||
1473 | */ |
||
1474 | 1 | public function stripeEmptyHtmlTags() |
|
1480 | |||
1481 | /** |
||
1482 | * Converts the string into an valid UTF-8 string. |
||
1483 | * |
||
1484 | * @return Stringy |
||
1485 | */ |
||
1486 | 1 | public function utf8ify() |
|
1490 | |||
1491 | /** |
||
1492 | * escape html |
||
1493 | * |
||
1494 | * @return Stringy |
||
1495 | */ |
||
1496 | 6 | public function escape() |
|
1506 | |||
1507 | /** |
||
1508 | * Create an extract from a text, so if the search-string was found, it will be centered in the output. |
||
1509 | * |
||
1510 | * @param string $search |
||
1511 | * @param int|null $length |
||
1512 | * @param string $ellipsis |
||
1513 | * |
||
1514 | * @return Stringy |
||
1515 | */ |
||
1516 | 1 | public function extractText($search = '', $length = null, $ellipsis = '...') |
|
1633 | |||
1634 | |||
1635 | /** |
||
1636 | * remove xss from html |
||
1637 | * |
||
1638 | * @return Stringy |
||
1639 | */ |
||
1640 | 6 | public function removeXss() |
|
1652 | |||
1653 | /** |
||
1654 | * remove html-break [br | \r\n | \r | \n | ...] |
||
1655 | * |
||
1656 | * @param string $replacement |
||
1657 | * |
||
1658 | * @return Stringy |
||
1659 | */ |
||
1660 | 6 | public function removeHtmlBreak($replacement = '') |
|
1666 | |||
1667 | /** |
||
1668 | * remove html |
||
1669 | * |
||
1670 | * @param $allowableTags |
||
1671 | * |
||
1672 | * @return Stringy |
||
1673 | */ |
||
1674 | 6 | public function removeHtml($allowableTags = null) |
|
1680 | |||
1681 | /** |
||
1682 | * Returns the substring beginning at $start, and up to, but not including |
||
1683 | * the index specified by $end. If $end is omitted, the function extracts |
||
1684 | * the remaining string. If $end is negative, it is computed from the end |
||
1685 | * of the string. |
||
1686 | * |
||
1687 | * @param int $start Initial index from which to begin extraction |
||
1688 | * @param int $end Optional index at which to end extraction |
||
1689 | * |
||
1690 | * @return Stringy Object with its $str being the extracted substring |
||
1691 | */ |
||
1692 | 18 | public function slice($start, $end = null) |
|
1708 | |||
1709 | /** |
||
1710 | * Splits the string with the provided regular expression, returning an |
||
1711 | * array of Stringy objects. An optional integer $limit will truncate the |
||
1712 | * results. |
||
1713 | * |
||
1714 | * @param string $pattern The regex with which to split the string |
||
1715 | * @param int $limit Optional maximum number of results to return |
||
1716 | * |
||
1717 | * @return Stringy[] An array of Stringy objects |
||
1718 | */ |
||
1719 | 19 | public function split($pattern, $limit = null) |
|
1753 | |||
1754 | /** |
||
1755 | * Surrounds $str with the given substring. |
||
1756 | * |
||
1757 | * @param string $substring The substring to add to both sides |
||
1758 | * |
||
1759 | * @return Stringy Object whose $str had the substring both prepended and |
||
1760 | * appended |
||
1761 | */ |
||
1762 | 5 | public function surround($substring) |
|
1768 | |||
1769 | /** |
||
1770 | * Returns a case swapped version of the string. |
||
1771 | * |
||
1772 | * @return Stringy Object whose $str has each character's case swapped |
||
1773 | */ |
||
1774 | 5 | public function swapCase() |
|
1782 | |||
1783 | /** |
||
1784 | * Returns a string with smart quotes, ellipsis characters, and dashes from |
||
1785 | * Windows-1252 (commonly used in Word documents) replaced by their ASCII |
||
1786 | * equivalents. |
||
1787 | * |
||
1788 | * @return Stringy Object whose $str has those characters removed |
||
1789 | */ |
||
1790 | 4 | public function tidy() |
|
1796 | |||
1797 | /** |
||
1798 | * Returns a trimmed string with the first letter of each word capitalized. |
||
1799 | * Also accepts an array, $ignore, allowing you to list words not to be |
||
1800 | * capitalized. |
||
1801 | * |
||
1802 | * @param array $ignore An array of words not to capitalize |
||
1803 | * |
||
1804 | * @return Stringy Object with a titleized $str |
||
1805 | */ |
||
1806 | 5 | public function titleize($ignore = null) |
|
1827 | |||
1828 | /** |
||
1829 | * Converts all characters in the string to lowercase. An alias for PHP's |
||
1830 | * UTF8::strtolower(). |
||
1831 | * |
||
1832 | * @return Stringy Object with all characters of $str being lowercase |
||
1833 | */ |
||
1834 | 27 | public function toLowerCase() |
|
1840 | |||
1841 | /** |
||
1842 | * Returns true if the string is base64 encoded, false otherwise. |
||
1843 | * |
||
1844 | * @return bool Whether or not $str is base64 encoded |
||
1845 | */ |
||
1846 | 7 | public function isBase64() |
|
1850 | |||
1851 | /** |
||
1852 | * Returns an ASCII version of the string. A set of non-ASCII characters are |
||
1853 | * replaced with their closest ASCII counterparts, and the rest are removed |
||
1854 | * unless instructed otherwise. |
||
1855 | * |
||
1856 | * @param $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance</p> |
||
1857 | * |
||
1858 | * @return Stringy Object whose $str contains only ASCII characters |
||
1859 | */ |
||
1860 | 16 | public function toAscii($strict = false) |
|
1866 | |||
1867 | /** |
||
1868 | * Returns a boolean representation of the given logical string value. |
||
1869 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
1870 | * 'off', and 'no' will return false. In all instances, case is ignored. |
||
1871 | * For other numeric strings, their sign will determine the return value. |
||
1872 | * In addition, blank strings consisting of only whitespace will return |
||
1873 | * false. For all other strings, the return value is a result of a |
||
1874 | * boolean cast. |
||
1875 | * |
||
1876 | * @return bool A boolean value for the string |
||
1877 | */ |
||
1878 | 15 | public function toBoolean() |
|
1900 | |||
1901 | /** |
||
1902 | * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
||
1903 | * |
||
1904 | * @return string |
||
1905 | */ |
||
1906 | 967 | public function toString() |
|
1910 | |||
1911 | /** |
||
1912 | * Converts each tab in the string to some number of spaces, as defined by |
||
1913 | * $tabLength. By default, each tab is converted to 4 consecutive spaces. |
||
1914 | * |
||
1915 | * @param int $tabLength Number of spaces to replace each tab with |
||
1916 | * |
||
1917 | * @return Stringy Object whose $str has had tabs switched to spaces |
||
1918 | */ |
||
1919 | 6 | View Code Duplication | public function toSpaces($tabLength = 4) |
1926 | |||
1927 | /** |
||
1928 | * Converts each occurrence of some consecutive number of spaces, as |
||
1929 | * defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
||
1930 | * are converted to a tab. |
||
1931 | * |
||
1932 | * @param int $tabLength Number of spaces to replace with a tab |
||
1933 | * |
||
1934 | * @return Stringy Object whose $str has had spaces switched to tabs |
||
1935 | */ |
||
1936 | 5 | View Code Duplication | public function toTabs($tabLength = 4) |
1943 | |||
1944 | /** |
||
1945 | * Converts the first character of each word in the string to uppercase. |
||
1946 | * |
||
1947 | * @return Stringy Object with all characters of $str being title-cased |
||
1948 | */ |
||
1949 | 5 | public function toTitleCase() |
|
1956 | |||
1957 | /** |
||
1958 | * Converts all characters in the string to uppercase. An alias for PHP's |
||
1959 | * UTF8::strtoupper(). |
||
1960 | * |
||
1961 | * @return Stringy Object with all characters of $str being uppercase |
||
1962 | */ |
||
1963 | 5 | public function toUpperCase() |
|
1969 | |||
1970 | /** |
||
1971 | * Returns a string with whitespace removed from the start of the string. |
||
1972 | * Supports the removal of unicode whitespace. Accepts an optional |
||
1973 | * string of characters to strip instead of the defaults. |
||
1974 | * |
||
1975 | * @param string $chars Optional string of characters to strip |
||
1976 | * |
||
1977 | * @return Stringy Object with a trimmed $str |
||
1978 | */ |
||
1979 | 13 | View Code Duplication | public function trimLeft($chars = null) |
1989 | |||
1990 | /** |
||
1991 | * Returns a string with whitespace removed from the end of the string. |
||
1992 | * Supports the removal of unicode whitespace. Accepts an optional |
||
1993 | * string of characters to strip instead of the defaults. |
||
1994 | * |
||
1995 | * @param string $chars Optional string of characters to strip |
||
1996 | * |
||
1997 | * @return Stringy Object with a trimmed $str |
||
1998 | */ |
||
1999 | 13 | View Code Duplication | public function trimRight($chars = null) |
2009 | |||
2010 | /** |
||
2011 | * Truncates the string to a given length. If $substring is provided, and |
||
2012 | * truncating occurs, the string is further truncated so that the substring |
||
2013 | * may be appended without exceeding the desired length. |
||
2014 | * |
||
2015 | * @param int $length Desired length of the truncated string |
||
2016 | * @param string $substring The substring to append if it can fit |
||
2017 | * |
||
2018 | * @return Stringy Object with the resulting $str after truncating |
||
2019 | */ |
||
2020 | 22 | View Code Duplication | public function truncate($length, $substring = '') |
2036 | |||
2037 | /** |
||
2038 | * Returns a lowercase and trimmed string separated by underscores. |
||
2039 | * Underscores are inserted before uppercase characters (with the exception |
||
2040 | * of the first character of the string), and in place of spaces as well as |
||
2041 | * dashes. |
||
2042 | * |
||
2043 | * @return Stringy Object with an underscored $str |
||
2044 | */ |
||
2045 | 16 | public function underscored() |
|
2049 | |||
2050 | /** |
||
2051 | * Returns an UpperCamelCase version of the supplied string. It trims |
||
2052 | * surrounding spaces, capitalizes letters following digits, spaces, dashes |
||
2053 | * and underscores, and removes spaces, dashes, underscores. |
||
2054 | * |
||
2055 | * @return Stringy Object with $str in UpperCamelCase |
||
2056 | */ |
||
2057 | 13 | public function upperCamelize() |
|
2061 | |||
2062 | /** |
||
2063 | * Returns a camelCase version of the string. Trims surrounding spaces, |
||
2064 | * capitalizes letters following digits, spaces, dashes and underscores, |
||
2065 | * and removes spaces, dashes, as well as underscores. |
||
2066 | * |
||
2067 | * @return Stringy Object with $str in camelCase |
||
2068 | */ |
||
2069 | 32 | public function camelize() |
|
2097 | |||
2098 | /** |
||
2099 | * Convert a string to e.g.: "snake_case" |
||
2100 | * |
||
2101 | * @return Stringy Object with $str in snake_case |
||
2102 | */ |
||
2103 | 20 | public function snakeize() |
|
2146 | |||
2147 | /** |
||
2148 | * Converts the first character of the string to lower case. |
||
2149 | * |
||
2150 | * @return Stringy Object with the first character of $str being lower case |
||
2151 | */ |
||
2152 | 37 | View Code Duplication | public function lowerCaseFirst() |
2161 | |||
2162 | /** |
||
2163 | * Shorten the string after $length, but also after the next word. |
||
2164 | * |
||
2165 | * @param int $length |
||
2166 | * @param string $strAddOn |
||
2167 | * |
||
2168 | * @return Stringy |
||
2169 | */ |
||
2170 | 4 | public function shortenAfterWord($length, $strAddOn = '...') |
|
2176 | |||
2177 | /** |
||
2178 | * Line-Wrap the string after $limit, but also after the next word. |
||
2179 | * |
||
2180 | * @param int $limit |
||
2181 | * |
||
2182 | * @return Stringy |
||
2183 | */ |
||
2184 | 1 | public function lineWrapAfterWord($limit) |
|
2196 | |||
2197 | /** |
||
2198 | * Gets the substring after the first occurrence of a separator. |
||
2199 | * If no match is found returns new empty Stringy object. |
||
2200 | * |
||
2201 | * @param string $separator |
||
2202 | * |
||
2203 | * @return Stringy |
||
2204 | */ |
||
2205 | 1 | View Code Duplication | public function afterFirst($separator) |
2221 | |||
2222 | /** |
||
2223 | * Gets the substring after the last occurrence of a separator. |
||
2224 | * If no match is found returns new empty Stringy object. |
||
2225 | * |
||
2226 | * @param string $separator |
||
2227 | * |
||
2228 | * @return Stringy |
||
2229 | */ |
||
2230 | 1 | View Code Duplication | public function afterLast($separator) |
2247 | |||
2248 | /** |
||
2249 | * Gets the substring before the first occurrence of a separator. |
||
2250 | * If no match is found returns new empty Stringy object. |
||
2251 | * |
||
2252 | * @param string $separator |
||
2253 | * |
||
2254 | * @return Stringy |
||
2255 | */ |
||
2256 | 1 | View Code Duplication | public function beforeFirst($separator) |
2273 | |||
2274 | /** |
||
2275 | * Gets the substring before the last occurrence of a separator. |
||
2276 | * If no match is found returns new empty Stringy object. |
||
2277 | * |
||
2278 | * @param string $separator |
||
2279 | * |
||
2280 | * @return Stringy |
||
2281 | */ |
||
2282 | 1 | View Code Duplication | public function beforeLast($separator) |
2299 | } |
||
2300 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: