Str
A fast, solid and strong typed string manipulation library with multibyte support
Install / Use
/learn @fe3dback/StrREADME
About
str/str
$str = new Str('Hello, 世界');
$str->last(2); // 世界
$str->chars(); // ['世', '界']
$str
->ensureLeft('Hello, ') // Hello, 世界
->ensureRight('!!!') // Hello, 世界!!!
->trimRight('!') // Hello, 世界
->prepend('Str say - '); // Str say - Hello, 世界
$send = function (string $s) {};
$send((string)$str); // same
$send($str->getString()); // same
Install
Requirements:
- php7.1
composer require str/str
Features
- [x] strongly typed
- [x] no exceptions thrown
- [x] fast
- [x] new functions
A fast string manipulation library with multi-byte support. Inspired by the "Stringy" library, with focus on speed.
Lib uses php7 features and does not throw any exceptions (because all input parameters are strongly typed). The code is completely covered by unit tests.
Functions Index:
A
- <a href="#afterfirst">afterFirst</a>
- <a href="#afterlast">afterLast</a>
- <a href="#append">append</a>
- <a href="#appenduniqueidentifier">appendUniqueIdentifier</a>
- <a href="#at">at</a>
B
- <a href="#beforefirst">beforeFirst</a>
- <a href="#beforelast">beforeLast</a>
- <a href="#between">between</a>
C
- <a href="#camelize">camelize</a>
- <a href="#chars">chars</a>
- <a href="#chop">chop</a>
- <a href="#collapsewhitespace">collapseWhitespace</a>
- <a href="#contains">contains</a>
- <a href="#containsall">containsAll</a>
- <a href="#containsany">containsAny</a>
- <a href="#countsubstr">countSubstr</a>
D
- <a href="#dasherize">dasherize</a>
- <a href="#delimit">delimit</a>
E
- <a href="#endswith">endsWith</a>
- <a href="#endswithany">endsWithAny</a>
- <a href="#ensureleft">ensureLeft</a>
- <a href="#ensureright">ensureRight</a>
F
- <a href="#first">first</a>
G
- <a href="#getstring">getString</a>
H
- <a href="#haslowercase">hasLowerCase</a>
- <a href="#hasprefix">hasPrefix</a>
- <a href="#hassuffix">hasSuffix</a>
- <a href="#hasuppercase">hasUpperCase</a>
- <a href="#htmldecode">htmlDecode</a>
- <a href="#htmlencode">htmlEncode</a>
- <a href="#humanize">humanize</a>
I
- <a href="#indexof">indexOf</a>
- <a href="#indexoflast">indexOfLast</a>
- <a href="#insert">insert</a>
- <a href="#isalpha">isAlpha</a>
- <a href="#isalphanumeric">isAlphanumeric</a>
- <a href="#isbase64">isBase64</a>
- <a href="#isblank">isBlank</a>
- <a href="#isemail">isEmail</a>
- <a href="#ishexadecimal">isHexadecimal</a>
- <a href="#isipv4">isIpV4</a>
- <a href="#isipv6">isIpV6</a>
- <a href="#isjson">isJson</a>
- <a href="#islowercase">isLowerCase</a>
- <a href="#isserialized">isSerialized</a>
- <a href="#isuuidv4">isUUIDv4</a>
- <a href="#isuppercase">isUpperCase</a>
J
- <a href="#join">join</a>
L
- <a href="#last">last</a>
- <a href="#length">length</a>
- <a href="#lines">lines</a>
- <a href="#longestcommonprefix">longestCommonPrefix</a>
- <a href="#longestcommonsubstring">longestCommonSubstring</a>
- <a href="#longestcommonsuffix">longestCommonSuffix</a>
- <a href="#lowercasefirst">lowerCaseFirst</a>
M
- <a href="#make">make</a>
- <a href="#matchespattern">matchesPattern</a>
- <a href="#move">move</a>
O
- <a href="#overwrite">overwrite</a>
P
- <a href="#padboth">padBoth</a>
- <a href="#padleft">padLeft</a>
- <a href="#padright">padRight</a>
- <a href="#pop">pop</a>
- <a href="#popreversed">popReversed</a>
- <a href="#prepend">prepend</a>
Q
- <a href="#quote">quote</a>
R
- <a href="#random">random</a>
- <a href="#regexreplace">regexReplace</a>
- <a href="#removeleft">removeLeft</a>
- <a href="#removeright">removeRight</a>
- <a href="#repeat">repeat</a>
- <a href="#replace">replace</a>
- <a href="#replacewithlimit">replaceWithLimit</a>
- <a href="#reverse">reverse</a>
S
- <a href="#safetruncate">safeTruncate</a>
- <a href="#shift">shift</a>
- <a href="#shiftreversed">shiftReversed</a>
- <a href="#shuffle">shuffle</a>
- <a href="#slice">slice</a>
- <a href="#slugify">slugify</a>
- <a href="#snakeize">snakeize</a>
- <a href="#split">split</a>
- <a href="#startswith">startsWith</a>
- <a href="#startswithany">startsWithAny</a>
- <a href="#stripwhitespace">stripWhitespace</a>
- <a href="#substr">substr</a>
- <a href="#surround">surround</a>
- <a href="#swapcase">swapCase</a>
T
- <a href="#tidy">tidy</a>
- <a href="#titleize">titleize</a>
- <a href="#toascii">toAscii</a>
- <a href="#toboolean">toBoolean</a>
- <a href="#tolowercase">toLowerCase</a>
- <a href="#tospaces">toSpaces</a>
- <a href="#totabs">toTabs</a>
- <a href="#totitlecase">toTitleCase</a>
- <a href="#touppercase">toUpperCase</a>
- <a href="#trim">trim</a>
- <a href="#trimleft">trimLeft</a>
- <a href="#trimright">trimRight</a>
- <a href="#truncate">truncate</a>
U
- <a href="#underscored">underscored</a>
- <a href="#unquote">unquote</a>
- <a href="#uppercamelize">upperCamelize</a>
- <a href="#uppercasefirst">upperCaseFirst</a>
W
- <a href="#words">words</a>
Functions List:
afterFirst
Inserts given $substr $times into the original string after the first occurrence of $needle.
$str = new Str('foo bar baz');
echo (string)$str->afterFirst('a', 'duh', 2);
// foo baduhduhr baz
Parameters:
- string $needle
- string $substr
- int $times
Return:
- \Str
afterLast
Inserts given $substr $times into the original string after the last occurrence of $needle.
$str = new Str('foo bar baz');
echo (string)$str->afterLast('a', 'duh', 2);
// foo bar baduhduhz
Parameters:
- string $needle
- string $substr
- int $times
Return:
- \Str
append
Append $sub to the string.
$str = new Str('/Acme');
echo (string)$str->append('/');
// /Acme/
Parameters:
- string $sub
Return:
- \Str
appendUniqueIdentifier
Appends a random string consisting of $possibleChars, if specified, of given $size or random length between $size and $sizeMax to the original string.
$str = new Str('foo');
echo $str->appendUniqueIdentifier(3, -1, 'foba_rz');
// foozro
Parameters:
- int $size
- int $sizeMax
- string $possibleChars
Return:
- \Str
at
Returns the character at $pos, with indexes starting at 0.
$str = new Str('/Acme/');
echo (string)$str->at(2);
// c
Parameters:
- int $pos
Return:
- \Str
beforeFirst
Inserts given $substr $times into the original string before the first occurrence of $needle.
$str = new Str('foo bar baz');
echo (string)$str->beforeFirst('a', 'duh');
// foo bduhar baz
Parameters:
- string $needle
- string $substr
- int $times
Return:
- \Str
beforeLast
Inserts given $substr $times into the original string before the last occurrence of $needle.
$str = new Str('foo bar baz');
echo (string)$str->beforeLast('a', 'duh');
// foo bar bduhaz
Parameters:
- string $needle
- string $substr
- int $times
Return:
- \Str
between
Returns the substring between $start and $end, if found, or an empty string. An optional $offset may be supplied from which to begin the search for the start string.
$str = new Str('/Acme/');
echo (string)$str->between('/', '/');
// Acme
Parameters:
- string $start
- string $end
- int $offset
Return:
- \Str
camelize
Returns a camelCase version of the string. Trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, as well as underscores.
$str = new Str('ac me');
echo (string)$str->camelize();
// acMe
Parameters: nothing
Return:
- \Str
chars
Returns an array consisting of the characters in the string.
$str = new Str('/Acme/');
echo (string)$str->chars();
// ['/', 'A', 'c', 'm', 'e', '/']
Parameters: nothing
Return:
- array
chop
Cuts the original string in pieces of $step size.
$str = new Str('foo bar baz');
echo $str->chop(2);
// ['fo', 'o ', 'ba', 'r ', 'ba', 'z']
Parameters:
- int $step
Return:
- array
collapseWhitespace
Trims the string and replaces consecutive whitespace characters with a single space. This includes tabs and newline characters, as well as multi-byte whitespace such as the thin space and ideographic space.
$str = new Str('foo bar baz');
echo (string)$str->collapseWhitespace();
// foo bar baz
Parameters: nothing
Return:
- \Str
contains
Check if the string contains $needle substring.
$str = new Str('/Acme/');
echo $str->contains('/');
// true
$str = new Str('/Acme/');
echo $str->contains('a', false);
// true
Parameters:
- string $needle
- bool $caseSensitive
Return:
- bool
containsAll
Returns true if the string contains all $needles, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$str = new Str('/Accmme/');
echo $str->containsAll(['m', 'c', '/']);
// true
Parameters:
- array $needles
- bool $caseSensitive
Return:
- bool
containsAny
Returns true if the string contains any $needles, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$str = new Str('/Accmme/');
echo $str->containsAny(['foo', 'c', 'bar']);
// true
Parameters:
- array $needles
- bool $caseSensitive
Return:
- bool
countSubstr
Returns the number of occurrences of $needle in the given string. By default the comparison is case-sensitive, but c
Related Skills
openhue
337.7kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
337.7kElevenLabs text-to-speech with mac-style say UX.
weather
337.7kGet current weather and forecasts via wttr.in or Open-Meteo
tweakcc
1.4kCustomize Claude Code's system prompts, create custom toolsets, input pattern highlighters, themes/thinking verbs/spinners, customize input box & user message styling, support AGENTS.md, unlock private/unreleased features, and much more. Supports both native/npm installs on all platforms.
