<?php
$res = Strings::from( " 12345 " )
->replace("/1/", "5")
->replace("/2/", "5")
->trim()
->substr(1, 3)
->get();
echo $res; // "534"- JavaScript-inspired methods
- Underscore.js-inspired methods
Return a new string consisting of the single UTF-16 code unit located at the specified offset into the string.
{string} $value- source string{string} $index- index of target character
charAt(string $value, int $index = 0): string<?php
$res = Strings::charAt("ABC", 1); // "B"Return an integer between 0 and 65535 representing the UTF-16 code unit at the given index
{string} $value- source string{string} $index- index of target character
charCodeAt(string $value, int $index = 0): int<?php
$res = Strings::charCodeAt("ABC", 0); // 65Concatenate the string arguments to the calling string and returns a new string.
{string} $value- source string{array} ...$strings- array of target strings
concat(string $value, ...$strings): string<?php
$res = Strings::concat("AB", "CD", "EF"); // ABCDEFDetermine whether a string ends with the characters of a specified string, returning true or false as appropriate.
{string} $value- source string{string} $search- substring to look for
endsWith(string $value, string $search): bool<?php
$res = Strings::endsWith("12345", "45"); // trueReturn a string created from the specified sequence of code units.
{array} $codes- array of char codes
fromCharCode(...$codes): string<?php
$res = Strings::fromCharCode(65, 66, 67); // ABC- Determine whether one string may be found within another string, returning true or false as appropriate.
{string} $value- source string{string} $search- substring to look for{int} $position- optionally, start position
includes(string $value, string $search, int $position = 0): bool<?php
$res = Strings::includes("12345", "1"); // trueReturn the index of the first occurrence of the specified value
{string} $value- source value{string} $searchStr- string to search for{int} $fromIndex- (optional) index to start with
indexOf(string $value, string $searchStr, int $fromIndex = 0): int<?php
$res = Strings::indexOf("ABCD", "BC"); // 1
$res = Strings::indexOf("ABCABC", "BC", 3); // 4Return the index of the last occurrence of the specified value
{string} $value- source value{string} $searchStr- string to search for{int} $fromIndex- (optional) index to start with
lastIndexOf(string $value, string $searchStr, int $fromIndex = 0): int<?php
$res = Strings::lastIndexOf("canal", "a"); // 3
$res = Strings::lastIndexOf("canal", "a", 2); // 1Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order
{string} $value- source value{string} $compareStr- string to compare with
localeCompare(string $value, string $compareStr): int<?php
\setlocale (LC_COLLATE, 'de_DE');
$res = Strings::localeCompare("a", "c"); // -2Retrieves the matches when matching a string against a regular expression.
{string} $value- source value{string} $regexp- regular expression to match
match(string $value, string $regexp): null|array<?php
$res = Strings::match("A1B1C1", "/[A-Z]/"); // ["A", "B", "C"]Pad the current string (to right) with a given string (repeated, if needed) so that the resulting string reaches a given length
{string} $value- source value{int} $length- padding length{string} $padString- (optional) a string to pad the source with
padEnd(string $value, int $length, string $padString = " "): string<?php
$res = Strings::padEnd("abc", 10); // "abc "
$res = Strings::padEnd("abc", 10, "foo"); // "abcfoofoof"Pad the current string (to left) with a given string (repeated, if needed) so that the resulting string reaches a given length
{string} $value- source value{int} $length- padding length{string} $padString- (optional) a string to pad the source with
padStart(string $value, int $length, string $padString = " "): string<?php
$res = Strings::padStart("abc", 10); // " abc"
$res = Strings::padStart("abc", 10, "foo"); // "foofoofabc"Remove substring from the string
{string} $value- source string{string} $search- substring to look for
remove(string $value, string $search): string<?php
$res = Strings::remove("12345", "1"); // "2345"Construct and return a new string which contains the specified number of copies of the string on which it was called, concatenated together.
{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
repeat(string $value, int $count): string<?php
$res = Strings::repeat("abc", 2); // abcabcPerform a regular expression search and replace
{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.
replace(string $value, string $pattern, string $replacement): string<?php
$res = Strings::replace("12345", "/\d/s", "*"); // "*****"Extract a section of a string and returns it as a new string.
{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.
slice(string $value, int $beginIndex, int $endIndex = null): stringSee also Difference between slice and substring
<?php
$res = Strings::slice("The morning is upon us.", 1, 8); // "he morn"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.
{string} $value- source string{string} $delimiter- specifies the string which denotes the points at which each split should occur.
split(string $value, string $delimiter): array<?php
$res = Strings::split("a,b,c", ","); // ["a", "b", "c"]Determine whether a string begins with the characters of a specified string, returning true or false as appropriate.
{string} $value- source string{string} $search- substring to look for
startsWith(string $value, string $search): bools<?php
$res = Strings::startsWith("12345", "12"); // trueReturn part of a string
{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).
substr(string $value, int $start, int $length = null): string<?php
$res = Strings::substr("12345", 1, 3); // "234"Return the part of the string between the start and end indexes, or to the end of the string.
{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.
substring(string $value, int $beginIndex, int $endIndex = null): stringSee also Difference between slice and substring
<?php
$value = "Mozilla";
$res = Strings::substring($value, 0, 1); // "M"
$res = Strings::substring($value, 1, 0); // "M"Return the calling string value converted to lower case.
{string} $value- source string{string} $mask- optionally, the stripped characters can also be specified using the parameter.
toLowerCase(string $value): string<?php
$res = Strings::toLowerCase("AbC"); // abcReturn the calling string value converted to upper case.
{string} $value- source string{string} $mask- optionally, the stripped characters can also be specified using the parameter.
toUpperCase(string $value): string<?php
$res = Strings::toUpperCase("AbC"); // ABCStrip whitespace (or other characters) from the beginning and end of a string
{string} $value- source string{string} $mask- optionally, the stripped characters can also be specified using the parameter.
trim(string $value, string $mask = " \t\n\r\0\x0B"): string<?php
$res = Strings::trim(" 12345 "); // "12345"Escapes a string for insertion into HTML
{string} $value- source string
escape(string $string): string<?php
$res = Strings::escape("Curly, Larry & Moe"); // "Curly, Larry & Moe"The opposite of escape
{string} $value- source string
unescape(string $string): string<?php
$res = Strings::unescape("Curly, Larry & Moe"); // "Curly, Larry & Moe"Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.
{string} $value- source string
chain(string $value): Strings<?php
$res = Strings::chain( " 12345 " )
->replace("/1/", "5")
->replace("/2/", "5")
->trim()
->substr(1, 3)
->value();
echo $res; // "534"