pataar / smartstring
A class to manipulate a string with a better syntax.
This package is not auto-updated.
Last update: 2024-09-28 19:44:10 UTC
README
Installation
Using Composer:
composer require pataar/smartstring
Usage
Creating a SmartString object
Either do
\Pataar\SmartString::create("Sample String");
Or
new \Pataar\SmartString("Sample String");
Using a SmartString object
After creating your SmartString, you can use several chainable methods to manipulate the string.
Some examples:
$sampletext = "Sample String"; echo \Pataar\SmartString::create($sampletext)->remove("String")->trim(); //which would echo "Sample" echo \Pataar\SmartString::create($sampletext)->prefix("A new ")->normalize(); //which would echo "a-new-sample-string" echo \Pataar\SmartString::create($sampletext)->toMd5WithSalt("Salting is good"); //which would echo "560fbd0056c4354c5dd0de0580c8c523" echo \Pataar\SmartString::create($sampletext)->remove("String")->trim()->toLower()->prefix("String "); //which would echo "String sample"
Functions
toUpper()
Creates an uppercase version of the string.
$smartString->toUpper();
toLower()
Creates an lowercase version of the string.
$smartString->toLower();
Trim()
Trims the string. So removes any trailing whitespaces and spaces.
$smartString->trim();
printme()
Prints the string.
$smartString->printme();
indexOf($input)
Returns the index of a certain $input string.
$smartString->indexOf($input);
substring($start, $length)
Creates a substring of an existing string.
$smartString->substring($start, $length);
toMd5()
Creates an MD5 hash of the string.
$smartString->toMd5();
toMd5WithSalt($salt)
Creates an salted MD5 hash of the string. Using both a prefix and suffix containing the given $salt
.
$smartString->toMd5WithSalt($salt);
encodeB64() || decodeB64()
Encodes or decodes a B64 string.
$smartString->encodeB64(); $smartString->decodeB64();
charAt($index)
Tells you on which index a certain character is placed.
$smartString->charAt($index);
equals($string)
Returns when the $string matches the $smartString.
$smartString->equals($string);
prefix($prefix)
Adds a $prefix to the string.
$smartString->prefix($prefix);
suffix($suffix)
Adds a $suffix to the string.
$smartString->suffix($suffix);
concat($string)
Concats a string.
$smartString->concat($string);
replace($search, $replacement)
Replace a string with an other string.
$smartString->replace($search, $replacement);
replacePattern($pattern, $replacement)
Replaces a pattern.
$smartString->replacePattern($pattern, $replacement);
remove($string)
Remove a string
$smartString->remove($string);
length()
Returns the length of the string.
$smartString->length();
normalize()
Normalizes the string. Making it URL/slug compatible.
$smartString->normalize();