在file_get_contents()php中插入Javascript


Insert Javascript in file_get_contents() php

有没有办法在file_get_contents()的末尾插入javascript?

我的意思不是添加到函数的输出中,javascript。我想要的是在我从file_get_contents()得到的网站的末尾放一个脚本。

就像使用chrome的扩展,它在网站的末尾插入一个javascript。但在我的情况下,我希望函数file_get_contents($url)直接输出所有内容(插入网站+javascript)。

在这种情况下有没有办法使用cUrl?

有办法做到这一点吗?

感谢

您需要对检索到的代码使用*_replace函数,或者使用DOM类来更改代码。无法修改file_get_contents本身以添加代码

$script =<<<END
   <script type="text/javascript" src="http://www.example.com/mysscript.js"></script>
   </body>
END;
$html = file_get_contents($url);
$html = str_replace("</body>",$script,$html);

这假设被抓取的内容有完整的html代码,主要包括</body>标签