PHP 程序用于在句子中用不同的符号替换单词
phpserver side programmingprogramming更新于 2025/8/23 4:22:17
要在句子中用不同的符号替换单词,PHP 代码如下 −
示例
<?php
$my_str = "This is a sample only";
$search_str = array("sample", "This");
$replace_str = array("simple");
$result = str_replace($search_str, $replace_str, $my_str);
print_r("The final replaced string is :");
print_r($result);
?>
输出
The final replaced string is : is a simple only
这里定义了一个字符串,需要替换成不同字符串的字符串放在一个数组中,并排列到一个变量中 −
$my_str = "This is a sample only";
$search_str = array("sample", "This");
$replace_str = array("simple");
‘str_replace’ 函数用于将值替换为不同的指定值。结果打印在屏幕上 −
$result = str_replace($search_str, $replace_str, $my_str);
print_r("The final replaced string is :");
print_r($result);

