开发者

Executing multiline sed in php: escaping issues

开发者 https://www.devze.com 2023-02-11 09:40 出处:网络
I am trying to run sed to do a multiline search and replace with the fol开发者_Python百科lowing string

I am trying to run sed to do a multiline search and replace with the fol开发者_Python百科lowing string

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

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);

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.


Probably the $ sign inside the $test variable makes PHP think that there is another variable that should be expanded.

Try escaping the $ character (\$), and have a look at the relevant PHP strings doc.

0

精彩评论

暂无评论...
验证码 换一张
取 消