Using PHP substr_replace() to Replace or Insert Substrings

This article explains the PHP substr_replace() function, its syntax, parameters, and demonstrates how to replace characters, replace substrings, and insert strings at the beginning, end, or middle of a string using practical code examples.

php Courses
php Courses
php Courses
Using PHP substr_replace() to Replace or Insert Substrings

The PHP substr_replace() function replaces a portion of a string with another string.

substr_replace() Function Syntax

substr_replace(string $string, string $replacement, int $start [, int $length]): string

Parameters: $string is the original string, $replacement is the string to insert, $start is the position where replacement begins, and optional $length specifies how many characters to replace. The function returns the modified string.

substr_replace() Function Examples

Replace characters at a specified position

$text = "Hello, world!";<br/>$text = substr_replace($text, "PHP", 7, 6);<br/>echo $text; // outputs "Hello, PHP!"

This example replaces six characters starting at position 7 with "PHP", resulting in "Hello, PHP!".

Replace a string at a specified position

$text = "Hello, world!";<br/>$text = substr_replace($text, "PHP", 7, 5);<br/>echo $text; // outputs "Hello, PHP!"

Here the length is set to 5, so the five characters from position 7 are replaced by "PHP".

Insert a string at the beginning of a string

$text = "world!";<br/>$text = substr_replace($text, "Hello, ", 0, 0);<br/>echo $text; // outputs "Hello, world!"

The string "Hello, " is inserted at the start, producing "Hello, world!".

Insert a string at the end of a string

$text = "Hello, ";<br/>$text = substr_replace($text, "world!", strlen($text), 0);<br/>echo $text; // outputs "Hello, world!"

By using the length of the original string as the start position, "world!" is appended to the end.

Insert a string in the middle of a string

$text = "Hello, world!";<br/>$text = substr_replace($text, "beautiful ", 7, 0);<br/>echo $text; // outputs "Hello, beautiful world!"

The word "beautiful " is inserted before position 7, resulting in "Hello, beautiful world!".

Summary

The substr_replace() function is a versatile tool for string manipulation in PHP, allowing replacement of characters or substrings at any position and insertion of strings at the beginning, middle, or end of a string.

PHP Learning Recommendations

Various tutorial links are provided for further learning.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Tutorialstring-manipulationsubstr_replace
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.