Quintin Par
Quintin Par

Reputation: 16262

Executing multiline sed in php: escaping issues

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 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?

Upvotes: 1

Views: 447

Answers (2)

Andrea Spadaccini
Andrea Spadaccini

Reputation: 12651

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.

Upvotes: 0

Tim
Tim

Reputation: 14164

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.

Upvotes: 3

Related Questions