Using Composer:
composer require pataar/smartstring
Either do
\Pataar\SmartString::create("Sample String");
Or
new \Pataar\SmartString("Sample String");
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"
Creates an uppercase version of the string.
$smartString->toUpper();
Creates an lowercase version of the string.
$smartString->toLower();
Trims the string. So removes any trailing whitespaces and spaces.
$smartString->trim();
Prints the string.
$smartString->printme();
Returns the index of a certain $input string.
$smartString->indexOf($input);
Creates a substring of an existing string.
$smartString->substring($start, $length);
Creates an MD5 hash of the string.
$smartString->toMd5();
Creates an salted MD5 hash of the string. Using both a prefix and suffix containing the given $salt
.
$smartString->toMd5WithSalt($salt);
Encodes or decodes a B64 string.
$smartString->encodeB64();
$smartString->decodeB64();
Tells you on which index a certain character is placed.
$smartString->charAt($index);
Returns when the $string matches the $smartString.
$smartString->equals($string);
Adds a $prefix to the string.
$smartString->prefix($prefix);
Adds a $suffix to the string.
$smartString->suffix($suffix);
Concats a string.
$smartString->concat($string);
Replace a string with an other string.
$smartString->replace($search, $replacement);
Replaces a pattern.
$smartString->replacePattern($pattern, $replacement);
Remove a string
$smartString->remove($string);
Returns the length of the string.
$smartString->length();
Normalizes the string. Making it URL/slug compatible.
$smartString->normalize();