在PHP中执行多行sed:转义问题

问题描述:

我正在尝试运行sed进行多行搜索,并替换为以下字符串

I am trying to run sed to do a multiline search and replace with the following string

$test = "sed -n '1h;1!H;${;g;s/iname=\"".$name.".*item>/".trim(xml)."/g;p;}' ".$file;
exec($test,$cmdresult);

选择

sed是因为要搜索的字符串超过10 mb.

sed is choice since the string to be searched is over 10 mb.

在执行过程中,编译器发出警告

During execution compiler issues a warning

PHP Parse error:  syntax error, unexpected ';' 

我该如何解决这个问题?

How do I go about solving this?

您需要对${}中的$进行转义.

You need to escape the $ in ${}.

$test = "sed -n '1h;1!H;\${;g;s/iname=\"".$name.".*item>/".trim(xml)."/g;p;}' ".$file;
exec($test,$cmdresult);

但是,为了让人们阅读您的代码,您应该真正地将字符串分割开.通过串联其他字符串sprintf或HEREDOC来创建它.

In order to let humans read your code, though, you should really split the string up. Create it by concatenating other strings, sprintf or HEREDOC.