Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

String Laravel Package

spatie/string

Fluent string handling for PHP. Wrap strings with string() to get a chainable object with helpers like between(), case conversion, concatenation, and array-offset access for reading/updating characters. Lightweight utility by Spatie, installable via Composer.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package via Composer:

composer require spatie/string

First Use Case: Transform and extract text from a user input field (e.g., a blog post title or description).

use Spatie\String\String;

// Basic usage: Wrap a string and chain methods
$title = String::make('  My Awesome Blog Post  ')->trim()->toLower()->slugify();
echo $title; // "my-awesome-blog-post"

// Extract content between delimiters
$content = String::make('Hello, world! This is a test.')
    ->between('Hello, ', 'test.')
    ->toUpper();
echo $content; // "world!"

Where to Look First:


Implementation Patterns

1. Fluent String Chaining

Leverage method chaining for readable, composable text transformations:

// Normalize and truncate a product description
$description = String::make($rawDescription)
    ->trim()
    ->replaceFirst('old', 'new')
    ->tease(100, '...')
    ->toHtml(); // Escape HTML entities if needed

Pattern: Use in Form Requests, Value Objects, or Service Layer methods to encapsulate logic:

// In a Form Request
public function rules(): array
{
    return [
        'title' => ['required', 'string', function ($attribute, $value, $fail) {
            $slug = String::make($value)->slugify();
            if (Post::where('slug', $slug)->exists()) {
                $fail('The '.$attribute.' has already been taken.');
            }
        }]
    ];
}

2. Array Access for Character Manipulation

Modify specific characters via offset notation:

// Fix typos or format text dynamically
$text = String::make('Hello, W0rld!');
$text[7] = 'r'; // Replace '0' with 'r'
$text[8] = 'l'; // Replace 'r' with 'l'
echo $text; // "Hello, World!"

Use Case: Dynamic form validation or text normalization (e.g., correcting common typos).


3. Underscore Integration for Advanced Text Processing

Combine Spatie’s methods with Underscore’s utilities (e.g., slugify(), camelCase()):

// Generate a URL-friendly slug from a title
$slug = String::make('User Profile: John Doe')
    ->slugify()
    ->prefix('user-')
    ->toLower();
echo $slug; // "user-user-profile-john-doe"

Pattern: Use in Model Observers, Accessors, or API Resources for consistent text formatting:

// In a Model Accessor
public function getFormattedNameAttribute()
{
    return String::make($this->name)
        ->trim()
        ->titleize()
        ->toHtml();
}

4. Text Extraction with between() and segment()

Extract substrings for data parsing or validation:

// Parse a filename or path
$filename = String::make('/uploads/images/photo_2023.jpg')
    ->segment('/', -1) // Last segment: "photo_2023.jpg"
    ->prefix('media/')
    ->toString();
echo $filename; // "media/photo_2023.jpg"

// Extract content between tags (e.g., for sanitization)
$cleanText = String::make('<p>Hello</p> <script>alert(1)</script>')
    ->between('<p>', '</p>')
    ->toHtml(); // "Hello"

Use Case: File upload handling, HTML sanitization, or log parsing.


5. Dynamic String Generation in Views/Blade

Use in Blade templates for localized or context-aware text:

{{ String::make($user->name)
    ->possessive()
    ->toHtml()
    ->prefix('Account of ')
}}

Output: "Account of John's"


Gotchas and Tips

Pitfalls

  1. PHP 8+ Requirement (v3.0.0+)

    • Ensure your project uses PHP 8+; older versions will fail.
    • Fix: Update php: in composer.json or use spatie/string:^2.2 for PHP 7 support.
  2. Non-String Inputs Throw Exceptions

    • Passing arrays/objects without __toString() will throw:
      String::make([]); // Throws \Spatie\String\Exceptions\InvalidStringException
      
    • Fix: Use (string) cast or toString():
      String::make((string) $nonStringObject);
      
  3. Underscore Methods May Not Chain

    • Methods like isEmail() return booleans and break chaining:
      String::make('test@example.com')->isEmail()->toUpper(); // Error!
      
    • Fix: Store intermediate results:
      $isValid = String::make($email)->isEmail();
      if ($isValid) { ... }
      
  4. possessive() Edge Cases

    • Fails on empty strings or "it" (returns "it's" instead of "its").
    • Fix: Add validation:
      if (String::make($word)->isNotEmpty() && $word !== 'it') {
          $possessive = String::make($word)->possessive();
      }
      
  5. Offset Assignment Overwrites Entire String

    • Assigning to an offset replaces the entire string if the offset is out of bounds:
      String::make('abc')->offsetSet(5, 'x'); // Returns "x"
      
    • Fix: Check bounds first or use substr() manually.

Debugging Tips

  1. Inspect Intermediate Steps Use toString() to debug chains:

    $step1 = String::make('  test  ')->trim()->toString(); // "test"
    $step2 = $step1->toUpper()->toString(); // "TEST"
    
  2. Leverage dd() for Complex Chains Break down long chains in Tinker or debuggers:

    dd(
        String::make($input)
            ->replaceFirst('old', 'new')
            ->segment('/', 1)
            ->toString()
    );
    
  3. Test Edge Cases

    • Empty strings, Unicode characters, or mixed delimiters (e.g., segment() with \n).
    • Example:
      String::make('')->between('a', 'b'); // Returns empty string
      String::make('café')->slugify(); // "cafe" (handles accents)
      

Extension Points

  1. Create Custom Methods Extend the String class to add domain-specific logic:

    // app/Extensions/StringExtensions.php
    namespace App\Extensions;
    
    use Spatie\String\String;
    
    class StringExtensions
    {
        public static function initialize()
        {
            String::macro('toTitleCase', function () {
                return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $this->toString())));
            });
        }
    }
    

    Register in AppServiceProvider:

    public function boot()
    {
        \App\Extensions\StringExtensions::initialize();
    }
    

    Usage:

    String::make('hello_world')->toTitleCase(); // "HelloWorld"
    
  2. Override Underscore Methods Replace or extend Underscore’s methods (e.g., customize slugify()):

    String::macro('customSlugify', function () {
        return $this->slugify()->replace(['-', '_'], '-');
    });
    
  3. Integrate with Laravel’s Helpers Add a global helper in composer.json:

    "extra": {
        "laravel": {
            "providers": ["Spatie\\String\\StringServiceProvider"]
        }
    }
    

    Now use string() globally:

    string('test')->toUpper(); // "TEST"
    

Performance Considerations

  1. Avoid Over-Chaining Each method call creates a new String instance. For heavy operations (e.g., bulk processing), minimize chains:
    // Less efficient
    foreach ($items as $item) {
        String::make($item)->slugify()->to
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle