Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add wordWrap to Str #48012

Merged
merged 7 commits into from Aug 9, 2023
Merged

[10.x] Add wordWrap to Str #48012

merged 7 commits into from Aug 9, 2023

Conversation

joshbonnick
Copy link
Contributor

This PR adds wordWrap the Stringable API.

What It Does

The method is a wrapper for the wordwrap function which is used to split strings with a given string by character length.

Without this change we would need to do the following to achieve the word wrapping

str(wordwrap($string, width: 20))

Use Cases

The PHP documentation for the function shows an example of adding <br /> elements into a string, however, that seems like a problem you solve with styling.

My own personal use case was to split a string into chunks so they could be used in an svg template, rendered by Blade to be passed to an image generating service.

The string would be split into chunks of 25 characters then passed to the template as a Collection (shown below).

public function using(string $title): self
{
    return tap($this, function() use ($title) {
        $this->title_parts = str($title)->wordWrap(characters: 25, break: PHP_EOL)->explode(PHP_EOL);
    });
}

@taylorotwell taylorotwell merged commit 99d004a into laravel:10.x Aug 9, 2023
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants