如何在php中替换短语?

时间:2016-07-20 09:47:08

标签: php replace str-replace

我尝试用函数str_replace替换php中的短语。它只取代两个字。例如:str_replace("noi that","noi thiet",$subject)

我想要替换短语> 2个字。例如:str_replace(“noi that Mien Nam”,“noi that Mien Bac”,$ subject)。但事实并非如此。你能帮助我吗?谢谢你

3 个答案:

答案 0 :(得分:1)

可能是您没有将转换后的字符串再次分配给字符串变量

<?php
$subject = "noi that Mien Nam now my test";
$subject = str_replace("noi that Mien Nam","noi that Mien Bac", $subject);
echo $subject ;
?>

检查一下:https://eval.in/608524其工作

如果您想用其他单词替换多个单词

使用此代码

<?php
$subject = "noi that Mien Nam now my test";
$src = array("noi that", "Mien Nam");
$dst   = array("noi teit", "Mien Bac");
echo $newsubject = str_replace($src, $dst, $subject);
?>

请检查:https://eval.in/608527

答案 1 :(得分:0)

嗨,请看这个例子并使用它是否有帮助

<?php
$subject = "my name is xyz abc i leave in jaipur";
$str =  str_replace("my name is xyz","hello dear what are", $subject); 
echo $str;

?>

答案 2 :(得分:0)

根据您的问题,我认为您想要从给定的String中替换多个字符串。以下是解决方案

$search  = array('programming', 'a PHP');
$replace = array('own', 'My');
$subject = 'This is a PHP programming language';
echo str_replace($search, $replace, $subject);
//Output:
//This is My own language