substr_replace

substr_replace(array|string $string, array|string $replace, array|int $offset, array|int|null $length = null): string|array

Replaces the portion of a string delimited by offset/length with a replacement string; omitting length replaces to the end, length 0 becomes pure insertion, a negative offset counts from the end - complementary to str_replace's global replacement.

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    char result[64];
    const char *s = "Hello World";
    // replace bytes [6, 11): prefix + replacement + suffix
    // (building into a fresh buffer, so no memmove is needed here)
    size_t off = 6, len = 5;
    memcpy(result, s, off);                  // prefix: "Hello "
    memcpy(result + off, "PHP", 3);          // replacement
    strcpy(result + off + 3, s + off + len); // suffix: "" (World was at the end)
    printf("%s\n", result);                  // Hello PHP
    return 0;
}
Output
Hello PHP

C++ C++26

C++26
#include <iostream>
#include <string>

int main() {
    std::string s = "Hello World";
    s.replace(6, 5, "PHP");   // std::string::replace is the direct counterpart
    std::cout << s << "\n";   // Hello PHP
    return 0;
}
Output
Hello PHP

C# v11.0.100

v11.0.100
// no direct counterpart — Substring concatenation
string s = "Hello World";
string r = s.Substring(0, 6) + "PHP" + s.Substring(6 + 5);
Console.WriteLine(r); // Hello PHP
Output
Hello PHP

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	s := "Hello World"
	// byte-slice splicing: prefix + replacement + suffix
	fmt.Println(s[:6] + "PHP" + s[6+5:]) // Hello PHP
	// insertion is a splice with an empty middle slice
	fmt.Println(s[:6] + "Beautiful " + s[6:]) // Hello Beautiful World
}
Output
Hello PHP
Hello Beautiful World

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // StringBuilder.replace is the direct counterpart (String itself is immutable)
        StringBuilder sb = new StringBuilder("Hello World");
        sb.replace(6, 11, "PHP");
        System.out.println(sb); // Hello PHP
    }
}
Output
Hello PHP

JavaScript v26.13.1

v26.13.1
// strings are immutable: splice with slice concatenation (splice() is for arrays)
const s = "Hello World"
console.log(s.slice(0, 6) + "PHP" + s.slice(11))       // Hello PHP
console.log(s.slice(0, 6) + "Beautiful " + s.slice(6)) // Hello Beautiful World
Output
Hello PHP
Hello Beautiful World

PHP v8.6.0

v8.6.0
echo substr_replace("Hello World", "PHP", 6, 5), "\n";        // Hello PHP
echo substr_replace("Hello World", "Beautiful ", 6, 0), "\n"; // length 0 = pure insertion
echo substr_replace("Hello World", "PHP", -5, 5), "\n";       // negative offset counts from the end
Output
Hello PHP
Hello Beautiful World
Hello PHP

Python v3.15.2

v3.15.2
s = "Hello World"
# plain slicing builds a new string
print(s[:6] + "PHP" + s[11:])  # Hello PHP
# slice assignment on a list is the exact in-place counterpart
chars = list(s)
chars[6:11] = "PHP"
print("".join(chars))          # Hello PHP
Output
Hello PHP
Hello PHP

Rust v1.99.1

v1.99.1
fn main() {
    let mut s = String::from("Hello World");
    s.replace_range(6..11, "PHP"); // replaces the byte range [6, 11)
    println!("{}", s); // Hello PHP

    let mut t = String::from("Hello World");
    t.insert_str(6, "Beautiful "); // a zero-length range is pure insertion
    println!("{}", t); // Hello Beautiful World
}
Output
Hello PHP
Hello Beautiful World

TypeScript v26.13.1

v26.13.1
// strings are immutable: splice with slice concatenation (splice() is for arrays)
const s: string = "Hello World";
console.log(s.slice(0, 6) + "PHP" + s.slice(11)); // Hello PHP
Output
Hello PHP

Language differences

C: no built-in - write prefix + replacement + suffix into a fresh buffer (memmove is only needed when replacing in place with overlap).

C++: std::string::replace is the direct counterpart.

C#: no direct counterpart - Substring concatenation.

Go: three-way byte-slice concatenation; indexes count bytes, so multibyte text needs rune slicing instead.

Java: StringBuilder.replace is the direct counterpart (String is immutable).

JavaScript: strings are immutable - slice and concatenate; splice is an array method with a confusingly similar name.

PHP: offsets are in bytes; omitting length replaces to the end, length 0 is pure insertion (the three demo lines show each case); array arguments batch-process multiple strings.

Python: slicing builds a new string; list slice assignment chars[a:b] = x is the exact in-place counterpart.

Rust: replace_range replaces a byte range in place; insert_str is pure insertion for a zero-length range.

TypeScript: same as JavaScript.