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

[8.x] Add Str::substrReplace() and Str::of($string)->substrReplace() methods for the PHP native substr_replace() function #39988

Merged
merged 7 commits into from
Dec 13, 2021

Conversation

ralphjsmit
Copy link
Contributor

@ralphjsmit ralphjsmit commented Dec 11, 2021

This pull request adds the ability to use the substr_replace() on the Str and Stringable classes.

What & why

Developers can now use directly use Str::substrReplace(...) and Str::of($string)->substrReplace(...).

Substring replace can be used to insert a string at a certain position in another string (set the last parameter, the length, to 0).

$string = '1300';

$result = Str::substrReplace($string, ':', 2, 0); 
// '13:00'

$result = (string) Str::of($string)->substrReplace(':', 2, 0);
// '13:00'

It can also be used to replace the remainder of a string after a certain position:

$result = (string) Str::of('Laravel Framework')->substrReplace('– The PHP Framework for Web Artisans', 8);
// 'Laravel – The PHP Framework for Web Artisans'

The benefit for webdevelopers is that they now have access to the substr_replace() method through the Str facade. The biggest benefit is (in my opinion), is that it's also a perfect candidate to be part of the fluent Stringable methods.

I'm also willing to PR a docs update for this, but I don't know if maintainers usually take care of updating the docs or if that is a task for contributors.

This is my first PR to Laravel, so please let me know if I missed anything or if there's anything I can improve. Thanks!

@GrahamCampbell GrahamCampbell changed the title Add Str::substrReplace() and Str::of($string)->substrReplace() methods for the PHP native substr_replace() function [8.x] Add Str::substrReplace() and Str::of($string)->substrReplace() methods for the PHP native substr_replace() function Dec 13, 2021
@taylorotwell taylorotwell merged commit a9d70e6 into laravel:8.x Dec 13, 2021
@ralphjsmit ralphjsmit deleted the substr-replace branch December 13, 2021 15:19
jaulz pushed a commit to jaulz/framework that referenced this pull request Dec 14, 2021
…()` methods for the PHP native `substr_replace()` function (laravel#39988)

* Add substr_replace Str / Stringable helpers

* Return a Stringable instead of a raw string

* Add tests

* Remove unused namespace

* Follow the advice of Style CI by adding a dot at the end of a docblock sentence

* Fix inconsistencies between PHP 7.3/7.4 and 8.0+

* Style
@NewEXE
Copy link

NewEXE commented Jan 9, 2022

It's NOT multibytes-ready.

$right = 'Ларавел – PHP-фреймворк';
$res = \Illuminate\Support\Str::substrReplace('Ларавел фреймворк', '– PHP-фреймворк', 8);
dump($right, $res);

// ^ "Ларавел – PHP-фреймворк"
// ^ "Лара– PHP-фреймворк"

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

4 participants