myerscode/utilities-strings
A small PHP utility library providing string helper functions for common formatting and manipulation tasks. Useful for Laravel or plain PHP projects to reduce boilerplate for trimming, case conversion, searching, and other everyday string operations.
Installation
composer require myerscode/utilities-strings
No additional configuration is required—just autoload the Myerscode\Utilities\Strings class.
First Use Case: Basic String Manipulation
use Myerscode\Utilities\Strings;
$string = Strings::create(' hello world ');
$result = $string->trim()->toLower()->replace(' ', '_');
echo $result; // Output: "hello_world"
Where to Look First
Strings class methods via IDE autocompletion (e.g., trim(), toLower(), replace(), slug(), pluralize()).create()->trim()->slug()).Strings::create() for new instances or Strings::from() for existing strings.Fluent String Chaining
$cleaned = Strings::create($userInput)
->trim()
->toLower()
->slug()
->replace(['-', '_'], ' ');
Conditional Logic with Methods
$string = Strings::create('test');
if ($string->startsWith('test')) {
$string->append(' case');
}
Integration with Laravel
public function rules()
{
return [
'name' => 'required|string',
'slug' => 'required|string',
];
}
public function prepareForValidation()
{
$this->merge([
'slug' => Strings::create($this->name)->slug()->value(),
]);
}
public function getFormattedNameAttribute()
{
return Strings::create($this->name)
->trim()
->titleCase()
->value();
}
Batch Processing
$strings = [' foo ', 'BAR', 'baz'];
$processed = array_map(
fn($s) => Strings::create($s)->trim()->toLower()->value(),
$strings
);
Custom Logic via Closures
$custom = Strings::create('example')
->transform(fn($str) => strrev($str))
->value(); // "elpmaxe"
Immutable Operations
trim() or replace() do not modify the original string; they return a new Strings instance.->value() to extract the result:
$original = ' test ';
$trimmed = Strings::create($original)->trim(); // $original remains unchanged
Case Sensitivity in Comparisons
startsWith() and endsWith() are case-sensitive by default.toLower()/toUpper() if needed:
if (Strings::create($input)->toLower()->startsWith('test')) { ... }
Performance with Large Strings
$temp = Strings::create($longString)->trim();
$result = $temp->replace(...)->value();
Locale-Specific Methods
titleCase() may not handle all locales perfectly (e.g., German umlauts).mb_* functions if needed:
$string->transform(fn($s) => mb_convert_case($s, MB_CASE_TITLE, 'UTF-8'));
Inspect Intermediate Values
Use ->value() to debug:
$step1 = Strings::create($input)->trim()->value();
dd($step1); // Debug after each transformation
Check for Null/Empty Inputs
The package may throw exceptions or return unexpected results if input is null or empty.
if (empty($input)) {
return Strings::create('default');
}
Override Default Behavior Extend the class for custom logic:
class CustomStrings extends Strings
{
public function customMethod()
{
return $this->transform(fn($s) => '[' . $s . ']');
}
}
Add Custom Methods Use traits or extend the class:
Strings::macro('customSlug', function() {
return $this->slug()->replace(['-', '_'], ' ');
});
Now use:
Strings::create('hello_world')->customSlug();
Integrate with Laravel Helpers
Create a helper function in app/Helpers/string.php:
if (!function_exists('cleanString')) {
function cleanString($str) {
return Strings::create($str)->trim()->toLower()->value();
}
}
Handle Edge Cases Override methods for specific needs (e.g., custom slug logic):
Strings::macro('customSlug', function() {
return $this->transform(fn($s) => Str::of($s)->slug('-'));
});
How can I help you explore Laravel packages today?