-2

我正在尝试通过以下代码替换字符串

$find2 = array ('/is/', '/working/'); 
 $replace2 = array ('to', 'work');
 $data="During the day, Damien is working";
 echo  preg_replace ($find2, $replace2, $data);

输出是

白天,达米安上班

但我希望结果是

达米安上班

4

3 回答 3

1

您应该将“白天”替换为“”。

于 2012-05-23T19:32:55.773 回答
1

要删除During the day, Damien is working,您可以使用以下命令:

$data = str_ireplace('During the day, Damien is working', 'Damien to work', $data);

以便:

$data = "During the day, Damien is working";
$data = str_ireplace('During the day, Damien is working', 'Damien to work', $data);
echo $data;

将回显(输出):

Damien to work

按照你的要求。

于 2012-05-23T19:36:46.767 回答
1

代码:

$data="During the day, Damien is working";
echo preg_replace("/.*,(.*)/i","$1",$data);

输出:

达米安在工作

会工作。它会将“任何东西,字符串”删除为“字符串”。删除“,”之前的所有内容以及“,”。


对于您的代码,您希望数据数组替换这两件事+据我所知,您还需要替换它。所以,

代码:

$find2 = array ('/is/', '/working/',"/.*,(.*)/"); 
 $replace2 = array ('to', 'work',"$1");
 $data="During the day, Damien is working";
 echo  preg_replace ($find2, $replace2, $data);

输出:

达米安上班

于 2012-05-23T19:42:57.007 回答