Skip to content

Latest commit

 

History

History
566 lines (449 loc) · 14.3 KB

File metadata and controls

566 lines (449 loc) · 14.3 KB

Dsheiko\Extras\Strings

Overview example

<?php
$res = Strings::from( " 12345 " )
            ->replace("/1/", "5")
            ->replace("/2/", "5")
            ->trim()
            ->substr(1, 3)
            ->get();
echo $res; // "534"

Methods

charAt

Return a new string consisting of the single UTF-16 code unit located at the specified offset into the string.

Parameters
  • {string} $value - source string
  • {string} $index - index of target character
Syntax
 charAt(string $value, int $index = 0): string
Example
<?php
$res = Strings::charAt("ABC", 1); // "B"

charCodeAt

Return an integer between 0 and 65535 representing the UTF-16 code unit at the given index

Parameters
  • {string} $value - source string
  • {string} $index - index of target character
Syntax
 charCodeAt(string $value, int $index = 0): int
Example
<?php
$res = Strings::charCodeAt("ABC", 0); // 65

concat

Concatenate the string arguments to the calling string and returns a new string.

Parameters
  • {string} $value - source string
  • {array} ...$strings - array of target strings
Syntax
 concat(string $value, ...$strings): string
Example
<?php
$res = Strings::concat("AB", "CD", "EF"); // ABCDEF

endsWith

Determine whether a string ends with the characters of a specified string, returning true or false as appropriate.

Parameters
  • {string} $value - source string
  • {string} $search - substring to look for
Syntax
 endsWith(string $value, string $search): bool
Example
<?php
$res = Strings::endsWith("12345", "45"); // true

fromCharCode

Return a string created from the specified sequence of code units.

Parameters
  • {array} $codes - array of char codes
Syntax
 fromCharCode(...$codes): string
Example
<?php
$res = Strings::fromCharCode(65, 66, 67); // ABC

includes

  • Determine whether one string may be found within another string, returning true or false as appropriate.
Parameters
  • {string} $value - source string
  • {string} $search - substring to look for
  • {int} $position - optionally, start position
Syntax
 includes(string $value, string $search, int $position = 0): bool
Example
<?php
$res = Strings::includes("12345", "1"); // true

indexOf

Return the index of the first occurrence of the specified value

Parameters
  • {string} $value - source value
  • {string} $searchStr - string to search for
  • {int} $fromIndex - (optional) index to start with
Syntax
 indexOf(string $value, string $searchStr, int $fromIndex = 0): int
Example
<?php
$res = Strings::indexOf("ABCD", "BC"); // 1
$res = Strings::indexOf("ABCABC", "BC", 3); // 4

lastIndexOf

Return the index of the last occurrence of the specified value

Parameters
  • {string} $value - source value
  • {string} $searchStr - string to search for
  • {int} $fromIndex - (optional) index to start with
Syntax
 lastIndexOf(string $value, string $searchStr, int $fromIndex = 0): int
Example
<?php
$res = Strings::lastIndexOf("canal", "a"); // 3
$res = Strings::lastIndexOf("canal", "a", 2); // 1

localeCompare

Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order

Parameters
  • {string} $value - source value
  • {string} $compareStr - string to compare with
Syntax
 localeCompare(string $value, string $compareStr): int
Example
<?php
\setlocale (LC_COLLATE, 'de_DE');
$res = Strings::localeCompare("a", "c"); // -2

match

Retrieves the matches when matching a string against a regular expression.

Parameters
  • {string} $value - source value
  • {string} $regexp - regular expression to match
Syntax
 match(string $value, string $regexp): null|array
Example
<?php
$res = Strings::match("A1B1C1", "/[A-Z]/"); // ["A", "B", "C"]

padEnd

Pad the current string (to right) with a given string (repeated, if needed) so that the resulting string reaches a given length

Parameters
  • {string} $value - source value
  • {int} $length - padding length
  • {string} $padString - (optional) a string to pad the source with
Syntax
 padEnd(string $value, int $length, string $padString = " "): string
Example
<?php
$res = Strings::padEnd("abc", 10); // "abc       "
$res = Strings::padEnd("abc", 10, "foo"); // "abcfoofoof"

padStart

Pad the current string (to left) with a given string (repeated, if needed) so that the resulting string reaches a given length

Parameters
  • {string} $value - source value
  • {int} $length - padding length
  • {string} $padString - (optional) a string to pad the source with
Syntax
 padStart(string $value, int $length, string $padString = " "): string
Example
<?php
$res = Strings::padStart("abc", 10); // "       abc"
$res = Strings::padStart("abc", 10, "foo"); // "foofoofabc"

remove

Remove substring from the string

Parameters
  • {string} $value - source string
  • {string} $search - substring to look for
Syntax
 remove(string $value, string $search): string
Example
<?php
$res = Strings::remove("12345", "1"); // "2345"

repeat

Construct and return a new string which contains the specified number of copies of the string on which it was called, concatenated together.

Parameters
  • {string} $value - source string
  • {int} $count - An integer between 0 and +∞: [0, +∞), indicating the number of times to repeat the string in the newly-created string that is to be returned
Syntax
 repeat(string $value, int $count): string
Example
<?php
$res = Strings::repeat("abc", 2); // abcabc

replace

Perform a regular expression search and replace

Parameters
  • {string} $value - source string
  • {string} $pattern - the pattern to search for. It can be either a string or an array with strings.
  • {string} $replacement - the string or an array with strings to replace.
Syntax
 replace(string $value, string $pattern, string $replacement): string
Example
<?php
$res = Strings::replace("12345", "/\d/s", "*"); // "*****"

slice

Extract a section of a string and returns it as a new string.

Parameters
  • {string} $value - source string
  • {int} $beginIndex - the zero-based index at which to begin extraction.
  • {int} $endIndex - (optional) the zero-based index before which to end extraction. The character at this index will not be included.
Syntax
 slice(string $value, int $beginIndex, int $endIndex = null): string

See also Difference between slice and substring

Example
<?php
$res = Strings::slice("The morning is upon us.", 1, 8); // "he morn"

split

Splits a string into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.

Parameters
  • {string} $value - source string
  • {string} $delimiter - specifies the string which denotes the points at which each split should occur.
Syntax
 split(string $value, string $delimiter): array
Example
<?php
$res = Strings::split("a,b,c", ","); // ["a", "b", "c"]

startsWith

Determine whether a string begins with the characters of a specified string, returning true or false as appropriate.

Parameters
  • {string} $value - source string
  • {string} $search - substring to look for
Syntax
 startsWith(string $value, string $search): bools
Example
<?php
$res = Strings::startsWith("12345", "12"); // true

substr

Return part of a string

Parameters
  • {string} $value - source string
  • {int} $start - start position
  • {int} $length - If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).
Syntax
 substr(string $value, int $start, int $length = null): string
Example
<?php
$res = Strings::substr("12345", 1, 3); // "234"

substring

Return the part of the string between the start and end indexes, or to the end of the string.

Parameters
  • {string} $value - source string
  • {int} $beginIndex - the zero-based index at which to begin extraction.
  • {int} $endIndex - (optional) the zero-based index before which to end extraction.
Syntax
 substring(string $value, int $beginIndex, int $endIndex = null): string

See also Difference between slice and substring

Example
<?php
$value = "Mozilla";
$res = Strings::substring($value, 0, 1); // "M"
$res = Strings::substring($value, 1, 0); // "M"

toLowerCase

Return the calling string value converted to lower case.

Parameters
  • {string} $value - source string
  • {string} $mask - optionally, the stripped characters can also be specified using the parameter.
Syntax
 toLowerCase(string $value): string
Example
<?php
$res = Strings::toLowerCase("AbC"); // abc

toUpperCase

Return the calling string value converted to upper case.

Parameters
  • {string} $value - source string
  • {string} $mask - optionally, the stripped characters can also be specified using the parameter.
Syntax
 toUpperCase(string $value): string
Example
<?php
$res = Strings::toUpperCase("AbC"); // ABC

trim

Strip whitespace (or other characters) from the beginning and end of a string

Parameters
  • {string} $value - source string
  • {string} $mask - optionally, the stripped characters can also be specified using the parameter.
Syntax
 trim(string $value, string $mask = " \t\n\r\0\x0B"): string
Example
<?php
$res = Strings::trim("  12345   "); // "12345"

escape

Escapes a string for insertion into HTML

Parameters
  • {string} $value - source string
Syntax
 escape(string $string): string
Example
<?php
$res = Strings::escape("Curly, Larry & Moe"); // "Curly, Larry &amp; Moe"

unescape

The opposite of escape

Parameters
  • {string} $value - source string
Syntax
 unescape(string $string): string
Example
<?php
$res = Strings::unescape("Curly, Larry &amp; Moe"); // "Curly, Larry & Moe"

chain

Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.

Parameters
  • {string} $value - source string
Syntax
 chain(string $value): Strings
Example
<?php
$res = Strings::chain( " 12345 " )
            ->replace("/1/", "5")
            ->replace("/2/", "5")
            ->trim()
            ->substr(1, 3)
            ->value();
echo $res; // "534"