dqitk20644 2012-08-16 10:07
浏览 210
已采纳

通过php system()传递多行html作为参数

Due to reasons that are have nothing to do with the actual question, I need to call and use an external script via PHP to perform a string replacement on a complete html document. The replacement strings and the source code need to be passded to this script via php exec(). For this example, I have used a simple python script to take over the replacement.

PHP script looks like this:

$source = file_get_contents("somehtmlfile.html");
$replaceString = "Some text in the HTML doc";
$replaceTo = "Some other text";
$parsedString = system("python replace.py $replaceString $replaceTo $source", $retval);
print ("Done:" .$mystring);

Then the Python script will do the following:

import sys
import string
dataFrom = sys.argv[1];
dataTo = sys.argv[2];
dataSourceCode = sys.argv[3];
rep = dataSourceCode.replace(dataFrom, dataTo);
print rep;

The problem is that I can't pass the complete html source as an argument to the shell, at least not in the way shown above. From what I understood, while the html code gets passed to the shell it interpretes some sections as commands (multiline could be an issue here I suppose).

The output I receive from the script :

sh: cannot open !DOCTYPE: No such file sh: cannot open html: No such file sh: cannot open head: No such file sh: cannot open title: No such file

... (this goes on)

Any suggestions?

  • 写回答

1条回答 默认 最新

  • dongxiaoguang9108 2012-08-16 13:17
    关注

    It is not working because there are spaces and quotes in the html text that you pass as an argument, so it is seen as multiple arguments. To solve this, you have to put quotes around the arguments.
    The correct code is $parsedString = system("python replace.py '$replaceString' '$replaceTo' '$source'", $retval);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂