alhames/string
A lightweight Laravel/PHP helper package providing convenient string utilities and shortcuts for common text operations such as formatting, cleaning, converting case, and working with substrings, making repetitive string handling tasks easier across your application.
Installation
composer require alhames/string
No configuration required—just autoload the facade or helper functions.
First Use Case Convert a string to kebab-case in a Blade template:
use Alhames\String\Facades\StringHelper;
{{ StringHelper::kebab('HelloWorldExample') }} // Output: "hello-world-example"
Or via helper:
kebab('HelloWorldExample'); // Same output
Where to Look First
String Sanitization
// Remove special chars from a slug
$cleanSlug = StringHelper::slug('Café au Lait!', '-');
// Output: "cafe-au-lait"
Case Conversion
// Convert API responses to readable format
$title = StringHelper::title('this_is_a_test');
// Output: "This Is A Test"
Text Wrapping
// Truncate long text in notifications
$shortened = StringHelper::limit('Lorem ipsum...', 20);
// Output: "Lorem ipsum..."
Validation Helpers
// Check if a string is alphanumeric in a Form Request
if (StringHelper::isAlphaNumeric($input)) { ... }
AppServiceProvider if extending:
public function boot()
{
$this->app->singleton(StringHelper::class, function ($app) {
return new StringHelper();
});
}
Blade::directive('kebab', function ($expression) {
return "<?php echo kebab({$expression}); ?>";
});
Usage:
@kebab($variable)
public function saving(Model $model)
{
$model->slug = StringHelper::slug($model->title);
}
Locale Sensitivity
slug() and title() may not handle non-Latin characters perfectly (e.g., Café → cafe vs. café). Test with your app’s supported locales.iconv:
$normalized = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
Helper vs. Facade Conflicts
str() helper vs. StringHelper::str()). Prefix facades in code:
use Facades\StringHelper as StrHelper;
Performance in Loops
StringHelper methods in tight loops (e.g., processing 10K records). Cache results or batch operations:
$strings = collect($records)->map(fn ($r) => StringHelper::slug($r->name))->toArray();
dd() to inspect intermediate strings:
dd(StringHelper::snake('Hello-World')); // Debug before final use
$fixed = mb_convert_encoding($string, 'UTF-8', 'auto');
Custom Methods Add a trait to extend functionality:
use Alhames\String\StringHelper;
class CustomStringHelper extends StringHelper {
public static function customMethod($str) {
return strtoupper($str) . '_SUFFIX';
}
}
Register the new class in AppServiceProvider.
Override Helpers Publish and modify the helper file:
composer dump-autoload
Then edit vendor/alhames/string/src/helpers.php.
Configuration
No built-in config, but you can create a config file (e.g., config/string.php) to store defaults like:
return [
'slug_separator' => '-',
'default_locale' => 'en_US',
];
Then bind it to the helper:
StringHelper::setConfig(config('string'));
StringHelper with Str::of() for chained operations:
Str::of($string)->kebab()->prepend('prefix_')->__toString();
StringHelper::snake() for consistency:
return response()->json(StringHelper::snake($model->toArray()));
$this->mock(StringHelper::class)->shouldReceive('slug')->andReturn('test-slug');
How can I help you explore Laravel packages today?