flextype-components / strings
Strings Component is a set of methods to help with the manipulation of strings.
Requires
- php: >=7.3.0
Requires (Dev)
- doctrine/coding-standard: 8.1.0
README
Installation
With Composer:
composer require flextype-components/strings
Usage
use Flextype\Component\Strings;
Methods
Method: Strings::stripSpaces()
Strip all whitespaces from the given string.
$string = Strings::stripSpaces('SG-1 returns from an off-world mission');
Method: Strings::trimSlashes()
Removes any leading and trailing slashes from a string.
$string = Strings::trimSlashes('some string here/');
Method: Strings::reduceSlashes()
Reduces multiple slashes in a string to single slashes.
$string = Strings::reduceSlashes('some//text//here');
Method: Strings::stripQuotes()
Removes single and double quotes from a string.
$string = Strings::stripQuotes('some "text" here');
Method: Strings::quotesToEntities()
Convert single and double quotes to entities.
$string = Strings::quotesToEntities('some "text" here');
Method: Strings::validEncoding()
Checks if the string is valid in UTF-8 encoding.
$result = Strings::validEncoding('An UTF-8 string here');
Method: Strings::fixEncoding()
Removes all invalid UTF-8 characters from a string.
$string = Strings::fixEncoding('An invalid UTF-8 string here');
Method: Strings::normalizeNewLines()
Standardize line endings to unix-like.
$string = Strings::normalizeNewLines('SG-1 returns from an off-world mission');
Method: Strings::normalizeSpaces()
Normalize white-spaces to a single space.
$string = Strings::normalizeSpaces('SG-1 returns from an off-world mission');
Method: Strings::random()
// Get random string with predefined settings $string = Strings::random(); // Get random string with custom length $string = Strings::random(10); // Get random string with custom length and custom keyspace $string = Strings::random(4, '0123456789');
Method: Strings::increment()
Add's _1
to a string or increment the ending number to allow _2
, _3
, etc.
// Increment string with predefined settings $string = Strings::increment('page_1'); // Increment string with custom settings $string = Strings::increment('page-1', 1, '-');
Method: Strings::wordsCount()
Return information about words used in a string
// Returns the number of words found $result = Strings::wordsCount('SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson'); // Returns an array containing all the words found inside the string $result = Strings::wordsCount('SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson', 1) // Returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself $result = Strings::wordsCount('SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson', 2)
Method: Strings::length()
Return the length of the given string.
$length = Strings::length('SG-1 returns from an off-world mission to P9Y-3C3');
Method: Strings::lower()
Convert the given string to lower-case.
$string = Strings::lower('SG-1 returns from an off-world mission to P9Y-3C3');
Method: Strings::upper()
Convert the given string to upper-case.
$string = Strings::upper('SG-1 returns from an off-world mission to P9Y-3C3');
Method: Strings::limit()
Limit the number of characters in a string.
// Get string with predefined limit settings $string = Strings::limit('SG-1 returns from an off-world mission to P9Y-3C3'); // Get string with limit 10 $string = Strings::limit('SG-1 returns from an off-world mission to P9Y-3C3', 10); // Get string with limit 10 and append 'read more...' $string = Strings::limit('SG-1 returns from an off-world mission to P9Y-3C3', 10, 'read more...');
Method: Strings::studly()
Convert a value to studly caps case.
$string = Strings::studly('foo_bar');
Method: Strings::snake()
Convert a string to snake case.
$string = Strings::snake('fooBar');
Method: Strings::camel()
Convert a string to camel case.
$string = Strings::camel('foo_bar');
Method: Strings::kebab()
Convert a string to kebab case.
$string = Strings::kebab('fooBar');
Method: Strings::words()
Limit the number of words in a string.
// Get the number of words in a string with predefined limit settings $string = Strings::words('SG-1 returns from an off-world mission to P9Y-3C3'); // Get the number of words in a string with limit 3 $string = Strings::words('SG-1 returns from an off-world mission to P9Y-3C3', 3); // Get the number of words in a string with limit 3 and append 'read more...' $string = Strings::words('SG-1 returns from an off-world mission to P9Y-3C3', 3, 'read more...');
Method: Strings::contains()
Determine if a given string contains a given substring.
// Determine if a given string contains a given substring. $result = Strings::contains('SG-1 returns from an off-world mission to P9Y-3C3', 'SG-1'); // Determine if a given string contains a given array of substrings. $result = Strings::contains('SG-1 returns from an off-world mission to P9Y-3C3', ['SG-1', 'P9Y-3C3']);
Method: Strings::containsAll()
Determine if a given string contains a given array of substrings.
$result = Strings::containsAll('SG-1 returns from an off-world mission to P9Y-3C3', ['SG-1', 'P9Y-3C3']);
Method: Strings::containsAny()
Determine if a given string contains any of array values.
$result = Strings::containsAny('SG-1 returns from an off-world mission to P9Y-3C3', ['SG-1', 'P9Y-3C3']);
Method: Strings::substr()
Returns the portion of string specified by the start and length parameters.
// Returns the portion of string specified by the start 0. $string = Strings::substr('SG-1 returns from an off-world mission to P9Y-3C3', 0); // Returns the portion of string specified by the start 0 and length 4. $string = Strings::substr('SG-1 returns from an off-world mission to P9Y-3C3', 0, 4);
Method: Strings::ucfirst()
Converts the first character of a string to upper case and leaves the other characters unchanged.
$string = Strings::ucfirst('daniel');
Method: Strings::trim()
Strip whitespace (or other characters) from the beginning and end of a string.
$string = Strings::trim(' daniel ');
Method: Strings::trimRight()
Strip whitespace (or other characters) from the end of a string.
$string = Strings::trimRight('daniel ');
Method: Strings::trimLeft()
Strip whitespace (or other characters) from the beginning of a string.
$string = Strings::trimLeft(' daniel');
Method: Strings::capitalize()
Converts the first character of every word of string to upper case and the others to lower case.
$string = Strings::capitalize('that country was at the same stage of development as the United States in the 1940s');
Method: Strings::reverse()
Reverses string.
$string = Strings::reverse('SG-1 returns from an off-world mission');
Method: Strings::segments()
Get array of segments from a string based on a delimiter.
// Get array of segments from a string based on a predefined delimiter. $segments = Strings::segments('SG-1 returns from an off-world mission'); // Get array of segments from a string based on a delimiter '-'. $segments = Strings::segments('SG-1 returns from an off-world mission', '-');
Method: Strings::segment()
Get a segment from a string based on a delimiter. Returns an empty string when the offset doesn't exist. Use a negative index to start counting from the last element.
// Get a segment 1 from a string based on a predefined delimiter. $string = Strings::segment('SG-1 returns from an off-world mission', 1); // Get a segment 1 from a string based on a delimiter '-'. $string = Strings::segment('SG-1 returns from an off-world mission', 1, '-'); // Get a segment 1 from a string starting from the last based on a delimiter '-'. $string = Strings::segment('SG-1 returns from an off-world mission', -1, '-');
Method: Strings::firstSegment()
Get the first segment from a string based on a delimiter.
// Get a first segment from a string based on a predefined delimiter. $string = Strings::firstSegment('SG-1 returns from an off-world mission'); // Get a first segment from a string based on a delimiter '-'. $string = Strings::firstSegment('SG-1 returns from an off-world mission', '-');
Method: Strings::lastSegment()
Get the last segment from a string based on a delimiter.
// Get a last segment from a string based on a predefined delimiter. $string = Strings::lastSegment('SG-1 returns from an off-world mission'); // Get a last segment from a string based on a delimiter '-'. $string = Strings::lastSegment('SG-1 returns from an off-world mission', '-');
Method: Strings::between()
Get the portion of a string between two given values.
$string = Strings::between('SG-1 returns from an off-world mission', 'SG-1', 'from');
Method: Strings::before()
Get the portion of a string before the first occurrence of a given value.
$string = Strings::before('SG-1 returns from an off-world mission', 'mission');
Method: Strings::beforeLast()
Get the portion of a string before the last occurrence of a given value.
$string = Strings::beforeLast('SG-1 returns from an off-world mission', 'mission');
Method: Strings::after()
Return the remainder of a string after the first occurrence of a given value.
$string = Strings::after('SG-1 returns from an off-world mission', 'SG-1');
Method: Strings::afterLast()
Return the remainder of a string after the last occurrence of a given value.
$string = Strings::afterLast('SG-1 returns from an off-world mission', 'SG-1');
Method: Strings::padBoth()
Pad both sides of a string with another.
$string = Strings::padBoth('SG-1 returns from an off-world mission', 50, '-');
Method: Strings::padRight()
Pad the right side of a string with another.
$string = Strings::padRight('SG-1 returns from an off-world mission', 50, '-');
Method: Strings::padLeft()
Pad the left side of a string with another.
$string = Strings::padLeft('SG-1 returns from an off-world mission', 50, '-');
Method: Strings::replaceArray()
Replace a given value in the string sequentially with an array.
$string = Strings::replaceArray('SG-1 returns from an off-world mission', 'SG-1', ['SG-2']);
Method: Strings::replaceFirst()
Replace the first occurrence of a given value in the string.
$string = Strings::replaceFirst('SG-1 returns from an off-world mission', 'SG-1', 'SG-2');
Method: Strings::replaceLast()
Replace the last occurrence of a given value in the string.
$string = Strings::replaceLast('SG-1 returns from an off-world mission', 'off-world', 'P9Y-3C3');
Method: Strings::start()
Begin a string with a single instance of a given value.
$string = Strings::start('movies/sg-1/season-5/episode-21/', '/');
Method: Strings::startsWith()
Determine if a given string starts with a given substring.
$result = Strings::startsWith('/movies/sg-1/season-5/episode-21/', '/');
Method: Strings::endsWith()
Determine if a given string ends with a given substring.
$result = Strings::endsWith('/movies/sg-1/season-5/episode-21/', '/');
Method: Strings::finish()
Cap a string with a single instance of a given value.
$result = Strings::finish('/movies/sg-1/season-5/episode-21', '/');
Method: Strings::hash()
Generate a hash string from the input string.
// Get string hash with predefined settings $result = Strings::hash('SG-1 returns from an off-world mission'); // Get string hash with hashed with sha256 algorithm $result = Strings::hash('SG-1 returns from an off-world mission', 'sha256'); // Get string hash with hashed with sha256 algorithm and with raw output $result = Strings::hash('SG-1 returns from an off-world mission', 'sha256', true);
License
The MIT License (MIT) Copyright (c) 2020 Sergey Romanenko