str_replace
str_replace(array|string $search, array|string $replace, array|string $subject, int &$count = null): array|string
将字符串中的指定子串替换为另一个子串。
C C23
C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "hello world world"; // world 出现两次:验证全部被替换
const char *from = "world";
const char *to = "PHP";
char out[64] = "";
size_t flen = strlen(from);
const char *cur = s;
const char *pos;
// 循环推进:替换每个匹配(等价于 PHP str_replace 默认行为)
while ((pos = strstr(cur, from)) != NULL) {
strncat(out, cur, pos - cur); // 匹配前的原文
strcat(out, to);
cur = pos + flen;
}
strcat(out, cur); // 剩余原文
printf("%s\n", out);
// 统计出现次数:PHP str_replace 的第 4 参报告的就是这个数
int hits = 0;
for (const char *p = s; (p = strstr(p, from)) != NULL; p += flen) hits++;
printf("%d occurrences found\n", hits);
return 0;
}
输出
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";
// 统计出现次数(等价 PHP str_replace 第 4 参)
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;
}
输出
hello PHP 2 occurrences found
C# v11.0.100
v11.0.100
Console.WriteLine("hello world".Replace("world", "PHP"));
// 批量:链式 Replace 调用模拟 PHP 的数组形态
Console.WriteLine("hello world".Replace("world", "PHP").Replace("hello", "bye"));
// 统计替换次数
var counted = "hello world world";
Console.WriteLine($"{counted.Split("world").Length - 1} occurrences found");
输出
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 是 str_replace 的默认行为;Count 给出替换次数
fmt.Println(strings.ReplaceAll("hello world", "world", "PHP"))
fmt.Println(strings.Count("hello world world", "world"), "occurrences found")
}
输出
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"));
// 链式替换模拟 PHP 的数组形态;用 split 长度统计次数
System.out.println("hello world".replace("world", "PHP").replace("hello", "bye"));
System.out.println("hello world world".split("world", -1).length - 1 + " occurrences found");
}
}
输出
hello PHP bye PHP 2 occurrences found
JavaScript v26.13.1
v26.13.1
console.log("hello world".replace("world", "PHP"))
// replaceAll(ES2021)替换全部出现;链式调用模拟数组形态
console.log("hello world world".replaceAll("world", "PHP"))
console.log("bye " + "hello world".replace("world", "PHP"))
输出
hello PHP hello PHP PHP bye hello PHP
PHP v8.6.0
v8.6.0
echo str_replace("world", "PHP", "hello world"), "\n";
// 数组形态按顺序应用规则:world->PHP,再 PHP->everyone
echo str_replace(["world", "PHP"], ["PHP", "everyone"], "hello world"), "
";
// 第 4 参统计替换发生次数
echo str_replace("o", "0", "hello world", $n), ": ", $n, " replaced";
输出
hello PHP hello everyone hell0 w0rld: 2 replaced
Python v3.15.2
v3.15.2
print("hello world".replace("world", "PHP"))
# 链式替换模拟 PHP 的数组形态;str.count 给出替换次数
print("hello world".replace("world", "PHP").replace("hello", "bye"))
print("hello world world".count("world"), "occurrences found")
输出
hello PHP bye PHP 2 occurrences found
Rust v1.99.1
v1.99.1
fn main() {
// replace 默认替换全部匹配(等价于 PHP str_replace 默认行为)
println!("{}", "hello world".replace("world", "PHP"));
// replacen 可限制次数;统计匹配数等价 str_replace 的第 4 参
println!("{}", "hello world world".replacen("world", "PHP", 1));
println!("{} occurrences found", "hello world world".matches("world").count());
}
输出
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")}`); // 链式替换
输出
hello PHP hello PHP PHP bye hello PHP
语言差异说明
C:以 strstr() 循环推进替换所有匹配(string.h 无内建替换函数)。
Go:用 strings.Replace(需显式传替换次数)。
Java:用 .replace()(替换全部)。
JavaScript:用 .replace();仅替换首个匹配,需加正则 /g 标志才会全部替换。
Python:用 .replace()。
Rust:的 replace 默认替换全部匹配,等价 PHP str_replace 的默认行为。