str_replace

str_replace(array|string $search, array|string $replace, array|string $subject, int &$count = null): array|string

Replaces occurrences of a substring with another substring.

C C23

C23
#include <stdio.h>
#include <string.h>
int main(void) {
    const char *s = "hello world world"; // two occurrences of world: verify all are replaced
    const char *from = "world";
    const char *to = "PHP";
    char out[64] = "";
    size_t flen = strlen(from);
    const char *cur = s;
    const char *pos;
    // advance in a loop: replace every match (equivalent to PHP str_replace's default behavior)
    while ((pos = strstr(cur, from)) != NULL) {
        strncat(out, cur, pos - cur); // original text before the match
        strcat(out, to);
        cur = pos + flen;
    }
    strcat(out, cur); // remaining original text
    printf("%s\n", out);
    // counting occurrences: PHP str_replace's 4th argument reports this count
    int hits = 0;
    for (const char *p = s; (p = strstr(p, from)) != NULL; p += flen) hits++;
    printf("%d occurrences found\n", hits);
    return 0;
}
Output
hello PHP PHP
2 occurrences found

C++ C++26

C++26
#include <iostream>
#include <string>
int main() {
    std::string s = "hello world";
    size_t pos = s.find("world");
    s.replace(pos, 5, "PHP");
    std::cout << s << "\n";
    // counting occurrences (PHP str_replace's 4th-argument equivalent)
    std::string text = "hello world world";
    int hits = 0;
    for (size_t p = text.find("world"); p != std::string::npos;
         p = text.find("world", p + 5)) hits++;
    std::cout << hits << " occurrences found\n";
    return 0;
}
Output
hello PHP
2 occurrences found

C# v11.0.100

v11.0.100
Console.WriteLine("hello world".Replace("world", "PHP"));
// batch: chained Replace calls mimic PHP's array form
Console.WriteLine("hello world".Replace("world", "PHP").Replace("hello", "bye"));
// counting replacements
var counted = "hello world world";
Console.WriteLine($"{counted.Split("world").Length - 1} occurrences found");
Output
hello PHP
bye PHP
2 occurrences found

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Replace("hello world", "world", "PHP", 1))

	// ReplaceAll is the str_replace default; Count gives the replacement count
	fmt.Println(strings.ReplaceAll("hello world", "world", "PHP"))
	fmt.Println(strings.Count("hello world world", "world"), "occurrences found")
}
Output
hello PHP
hello PHP
2 occurrences found

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        System.out.println("hello world".replace("world", "PHP"));
        // chained replaces mimic PHP's array form; counting via split length
        System.out.println("hello world".replace("world", "PHP").replace("hello", "bye"));
        System.out.println("hello world world".split("world", -1).length - 1 + " occurrences found");
    }
}
Output
hello PHP
bye PHP
2 occurrences found

JavaScript v26.13.1

v26.13.1
console.log("hello world".replace("world", "PHP"))
// replaceAll (ES2021) replaces every occurrence; chained calls mimic the array form
console.log("hello world world".replaceAll("world", "PHP"))
console.log("bye " + "hello world".replace("world", "PHP"))
Output
hello PHP
hello PHP PHP
bye hello PHP

PHP v8.6.0

v8.6.0
echo str_replace("world", "PHP", "hello world"), "\n";
// array form applies rules in sequence: world->PHP then PHP->everyone
echo str_replace(["world", "PHP"], ["PHP", "everyone"], "hello world"), "
";
// 4th argument counts the replacements made
echo str_replace("o", "0", "hello world", $n), ": ", $n, " replaced";
Output
hello PHP
hello everyone
hell0 w0rld: 2 replaced

Python v3.15.2

v3.15.2
print("hello world".replace("world", "PHP"))
# chained replaces mimic PHP's array form; str.count gives the replacement count
print("hello world".replace("world", "PHP").replace("hello", "bye"))
print("hello world world".count("world"), "occurrences found")
Output
hello PHP
bye PHP
2 occurrences found

Rust v1.99.1

v1.99.1
fn main() {
    // replace replaces all matches by default (equivalent to PHP str_replace's default behavior)
    println!("{}", "hello world".replace("world", "PHP"));

    // replacen limits the count; counting matches mimics str_replace's 4th arg
    println!("{}", "hello world world".replacen("world", "PHP", 1));
    println!("{} occurrences found", "hello world world".matches("world").count());
}
Output
hello PHP
hello PHP world
2 occurrences found

TypeScript v26.13.1

v26.13.1
console.log("hello world".replace("world", "PHP"));
console.log("hello world world".replaceAll("world", "PHP")); // replaceAll (ES2021)
console.log(`bye ${"hello world".replace("world", "PHP")}`); // chained replaces
Output
hello PHP
hello PHP PHP
bye hello PHP

Language differences

C: replaces all matches by looping strstr() (string.h has no built-in replace).

Go: uses strings.Replace (the replacement count must be passed explicitly).

Java: uses .replace() (replaces all).

JavaScript: uses .replace(); it only replaces the first match unless given a regex with the /g flag.

Python: uses .replace().

Rust: replace replaces all matches by default, matching PHP str_replace behavior.