mscribellito / str
Str is an immutable PHP class that provides convenient, object-oriented operations for string handling and manipulation.
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ^8.1
This package is auto-updated.
Last update: 2024-04-12 01:02:19 UTC
README
_____ _
/ ____|| |
| (___ | |_ _ __
\___ \ | __|| '__|
____) || |_ | |
|_____/ \__||_|
What is Str?
Str is an immutable PHP class that provides convenient, object-oriented operations for string handling and manipulation. Str provides methods for examining individual characters of the string, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. A Str is immutable (constant) and its value cannot be changed after creation.
Note: Str is not intended to replace all instances of your string variables - just those of which require many string operations and can benefit from an easy to use API.
Requirements
PHP version 7 or newer is required.
Installing
Install via Composer:
composer require mscribellito/str
Require:
require 'path/to/Str.php';
Example Usage
$lipsum = new Str("Lorem ipsum dolor sit amet"); $search = "ipsum"; if ($lipsum->contains($search)) { printf("'%s' contains '%s'", $lipsum, $search); // 'Lorem ipsum dolor sit amet' contains 'ipsum' }
You can also create an instance of Str via a convenient, helper function:
$lipsum = Str("Lorem ipsum dolor sit amet");
Chaining
$str = new Str('php'); echo $str->toUpperCase()->concat(' is a popular general-purpose scripting language'); // PHP is a popular general-purpose scripting language
Constructor Summary
Constructor and Description |
---|
Str() Initializes a newly created Str object so that it represents an empty character sequence. |
Str(mixed $original) Initializes a newly created Str object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. |
Str(mixed $original, int $offset, int $count) Initializes a newly created Str object so that it contains characters from a substring of the argument string. |
Method Summary
Modifier and Type | Method and Description |
---|---|
string |
__toString() The value of this string is returned. |
string |
charAt(int $index) Returns the character at the specified index. |
int |
charCodeAt(int $index) Returns the character ASCII value at the specified index. |
int |
compareTo(string $str) Compares two strings lexicographically. |
int |
compareToIgnoreCase(string $str) Compares two strings lexicographically, ignoring case differences. |
Str |
concat() Concatenates the specified string(s) to the end of this string. |
bool |
contains(string $str) Returns true if and only if this string contains the specified string. |
bool |
endsWith(string $suffix) Tests if this string ends with the specified suffix. |
bool |
equals(string $str) Compares this string to the specified string. |
bool |
equalsIgnoreCase(string $str) Compares this string to the specified string, ignoring case considerations. |
static Str |
format(string $format) Returns a formatted string using the specified format string and arguments. |
int |
indexOf(string $str, [int $fromIndex=0]) Returns the index within this string of the first occurrence of the specified string, optionally starting the search at the specified index. |
bool |
isEmpty() Returns true if and only if length() is 0. |
static Str |
join(string $delimiter, mixed[] $elements) Returns a new string composed of array elements joined together with the specified delimiter. |
int |
lastIndexOf(string $str, [int $fromIndex=0]) Returns the index within this string of the last occurrence of the specified character, optionally starting the search at the specified index. |
int |
length() Returns the length of this string. |
bool |
matches(string $regex) Tells whether or not this string matches the given regular expression. |
bool |
regionMatches(int $toffset, string $str, int $ooffset, int $length, [bool $ignoreCase=false]) Tests if two string regions are equal. |
Str |
replace(string $target, string $replacement) Returns a string resulting from replacing all occurrences of target in this string with replacement. |
Str |
replaceAll(string $regex, string $replacement) Replaces each substring of this string that matches the given regular expression with the given replacement. |
Str |
replaceFirst(string $regex, string $replacement) Replaces the first substring of this string that matches the given regular expression with the given replacement. |
Str[] |
split(string $regex, [int $limit=null]) Splits this string around matches of the given regular expression. |
bool |
startsWith(string $prefix, [int $toffset=0]) Tests if this string starts with the specified prefix, optionally starting the search at the specified index. |
Str |
substring(int $beginIndex, [int $endIndex=null]) Returns a string that is a substring of this string. |
string[] |
toCharArray() Converts this string to a new character array. |
Str |
toLowerCase() Converts all of the characters in this string to lower case. |
Str |
toUpperCase() Converts all of the characters in this string to upper case. |
Str |
trim([string $characterMask=" \t\n\r\0\x0B"]) Returns a string whose value is this string, with any leading and trailing whitespace removed. |
Testing
Run tests with vendor/bin/phpunit
License
Released under the MIT License. See LICENSE for details.