-4

I need to replace two words with one word in string. For example my string looks like:

$mySentence = "abcd efg ijkl";
$replaceWith = "XYZ";
$newString = str_replace("efg ijkl", $replaceWith, $mySentence);

But there is no any changes.

The output should be abcd XYZ.

4

4 回答 4

2
$mySentence = "abcd efg ijkl";
$replaceWith = "XYZ";
$newString = str_replace("efg ijkl", $replaceWith, $mySentence);
echo $newString;

这输出

abcd XYZ
于 2013-08-14T09:32:55.547 回答
1

但是没有任何变化。

变化,但你只是不输出它来查看。您正在分配$newString替换的文本,但从不显示新字符串。

因此,正如人们所回答的那样,您只需要输出变量即可。通过echovar_dump()

echo $newString

会给你

abcd XYZ
于 2013-08-14T09:36:52.480 回答
0

代码:

<?php
$mySentence = "abcd efg ijkl";
$replaceWith = "XYZ";
$newString = str_replace("efg ijkl", $replaceWith, $mySentence);
echo $newString;
?>
于 2013-08-14T09:33:47.337 回答
0
 $mySentence = "abcd efg ijkl";
 $replaceWith = "XYZ";
 $newString = preg_replace("/efg ijkl/", $replaceWith, $mySentence);
 print_r($newString);

输出:

abcd XYZ
于 2013-08-14T09:34:01.827 回答