Backend Development 4 min read

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

<code>substr_replace(string $string, string $replacement, int $start [, int $length]): string</code>

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

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

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

Replace a string at a specified position

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

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

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

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

Insert a string at the end of a string

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

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

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

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.

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

login 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.