yiisoft/strings
Yii Strings provides fast, multibyte-safe string utilities for PHP: StringHelper and NumericHelper, Inflector (pluralize, slug), wildcard pattern matching, and optimized combined regex matching with optional memoization.
Installation:
composer require yiisoft/strings
No additional configuration is required—this is a lightweight, dependency-free package.
First Use Case:
use Yiisoft\Strings\StringHelper;
// Basic string manipulation
$string = StringHelper::lower('Hello WORLD');
echo $string; // "hello world"
// Common operations
$trimmed = StringHelper::trim(' extra spaces ');
$slug = StringHelper::slugify('Laravel Assessment');
Where to Look First:
StringHelper—this is the sole class in the package.String Sanitization:
// Remove HTML tags
$clean = StringHelper::stripTags('<p>Hello</p>');
// Escape for HTML
$escaped = StringHelper::htmlspecialchars('User <script>alert()</script>');
Text Transformation:
// Convert to title case
$title = StringHelper::titleCase('laravel is awesome');
// Convert to kebab-case
$kebab = StringHelper::kebabCase('LaravelAssessment');
Validation & Checks:
// Check if string is alphanumeric
if (StringHelper::isAlphanumeric('abc123')) {
// ...
}
// Check if string is a valid email
if (StringHelper::isEmail('test@example.com')) {
// ...
}
Localization & Encoding:
// Convert to ASCII (transliterate)
$ascii = StringHelper::ascii('Café');
// Normalize Unicode (e.g., NFD form)
$normalized = StringHelper::normalize('résumé');
// app/Helpers/StringHelper.php
if (!class_exists('StringHelper')) {
require app_path('Helpers/StringHelper.php');
}
// In Blade: @php echo StringHelper::slugify($title) @endphp
public function rules()
{
return [
'name' => ['string', 'filter' => [StringHelper::class, 'slugify']],
];
}
return response()->json([
'title' => StringHelper::titleCase($request->title),
]);
Encoding Assumptions:
StringHelper::ascii() may not handle all Unicode characters perfectly. Test with non-Latin scripts (e.g., Cyrillic, CJK).StringHelper::transliterate() for broader compatibility.Case Sensitivity:
StringHelper::slugify() is case-sensitive by default. Override with:
StringHelper::slugify('Laravel', ['lower' => true]);
Performance:
normalize() or transliterate() on large datasets (e.g., database bulk updates) can be slow.Edge Cases:
StringHelper::isEmail() uses a basic regex. For strict validation, combine with Laravel’s ValidatesEmail.stripTags('') returns '', but slugify('') returns '-').Method Chaining:
$result = StringHelper::lower($str)
->slugify()
->trim();
Note: The package doesn’t support chaining natively—use intermediate variables or a helper method.
Custom Rules: Extend functionality by creating a wrapper:
class CustomStringHelper extends StringHelper {
public static function customSlugify($string): string {
return parent::slugify($string, ['replacement' => '-', 'lower' => true]);
}
}
Add Custom Replacements:
$customReplacements = [
'foo' => 'bar',
'baz' => 'qux',
];
StringHelper::slugify($str, ['replacements' => $customReplacements]);
Override Default Config: The package uses static methods, so "configuration" is passed via method arguments. For reusable settings, create a config class:
class StringConfig {
public static function slugifyOptions(): array {
return ['replacement' => '_', 'lower' => true];
}
}
// Usage: StringHelper::slugify($str, StringConfig::slugifyOptions())
Testing:
Mock StringHelper in PHPUnit:
$this->partialMock(StringHelper::class, ['slugify'])
->method('slugify')
->willReturn('mocked-slug');
How can I help you explore Laravel packages today?