pragmarx/ia-str
Framework-agnostic extraction of Laravel’s Illuminate\Support\Str and string helper functions, repackaged under IlluminateAgnostic\Str to avoid namespace conflicts. Use Str:: methods or global str_* helpers in any PHP project, including Laravel apps.
Installation:
composer require pragmarx/ia-str
Add to composer.json if not using autoloading:
"autoload": {
"psr-4": {
"App\\": "app/",
"IlluminateAgnostic\\": "vendor/pragmarx/ia-str/src"
}
}
First Use Case:
Replace native string operations with Str helpers in a non-Laravel project or to avoid Laravel namespace conflicts:
use IlluminateAgnostic\Str\Support\Str;
// Basic usage
$result = Str::contains('Hello World', 'World'); // true
$slug = Str::slug('Hello World'); // "hello-world"
Where to Look First:
vendor/pragmarx/ia-str/src/Support/Str.php for all available methods.tests/ for usage examples and edge cases.String Manipulation:
Use Str for consistent string operations across projects:
// Camel case, snake case, kebab case
Str::camel('hello_world'); // "helloWorld"
Str::snake('helloWorld'); // "hello_world"
Str::kebab('helloWorld'); // "hello-world"
// Trimming and padding
Str::padLeft('hi', 5, '='); // "===hi"
Str::trim(' hello '); // "hello"
URL and Path Handling: Leverage Laravel’s built-in URL helpers without Laravel’s routing:
Str::of('https://example.com/path?query=1')
->after('example.com/')
->before('?'); // "path"
Character and Word Operations:
// Substring, word counting, etc.
Str::limit('Hello World', 5); // "Hello..."
Str::words('Hello World', 1); // ["Hello"]
Str
->of('Hello World')
->replaceFirst('World', 'Laravel')
->value(); // "Hello Laravel"
Macroable Methods:
Extend Str with custom logic:
Str::macro('reverse', function ($string) {
return strrev($string);
});
Str::reverse('hello'); // "olleh"
Integration with Collections:
Use Str with Illuminate\Support\Collection (if installed separately):
collect(['Hello', 'World'])
->map(fn ($str) => Str::upper($str))
->values(); // ["HELLO", "WORLD"]
Str::of($string) for fluent chaining to avoid repeated string literals.strtolower()) over Str helpers if micro-optimizations are critical.Str in unit tests to isolate string logic:
$this->partialMock(Str::class, ['slug'])
->shouldReceive('slug')
->with('Test String')
->andReturn('test-string');
Namespace Conflicts:
IlluminateAgnostic\Str\Support\Str to avoid collisions:
use Illuminate\Support\Facades\Str as LaravelStr;
use IlluminateAgnostic\Str\Support\Str as AgnosticStr;
composer.json autoloading.Deprecated Methods:
Str::ascii()) rely on voku/portable-ascii. Ensure compatibility:
composer require voku/portable-ascii
Case Sensitivity:
Str::contains() are case-sensitive by default. Use Str::lower() or Str::upper() for case-insensitive checks:
Str::contains(Str::lower($haystack), Str::lower($needle));
Locale-Specific Operations:
Str::title()) may not handle Unicode/locale-specific rules perfectly. Test with edge cases like:
Str::title('jönköping'); // "Jönköping" (may vary by PHP locale)
Fluent Method Chaining:
Str::of()->upper()->slug() is efficient but can be less readable for complex logic. Break into steps if needed:
$upper = Str::of($str)->upper()->value();
$slug = Str::slug($upper);
get_class_methods(Str::class) to list available methods.Str::of('')->slug()).null, arrays). Str will cast them to strings.Str::ascii('café')).Custom Helpers:
Add static methods to Str for project-specific needs:
Str::macro('abbreviate', function ($string, $length = 20) {
return Str::length($string) > $length
? Str::substr($string, 0, $length - 3) . '...'
: $string;
});
Override Existing Methods: Use traits or extend the class (though this requires deeper integration):
class CustomStr extends \IlluminateAgnostic\Str\Support\Str {
public static function customMethod($string) { ... }
}
Configuration:
strtolower()), native PHP functions are faster. Reserve Str for complex or reusable logic.Str::slug()) if called repeatedly:
$cache = [];
Str::macro('cachedSlug', function ($string) use (&$cache) {
return $cache[$string] ?? $cache[$string] = Str::slug($string);
});
How can I help you explore Laravel packages today?