PHP简单的HTML DOM解析器:我想用php简单的dom解析器添加到html dom的javascript链接

时间:2015-12-10 09:54:37

标签: php html dom

我使用PHP Simple HTML DOM Parser,我想添加javascript链接到html dom。 这是我的代码。

$html=  file_get_contents("http://example.com")

我想添加

<script type="text/javascript" src="http://exapmle.com/example.js"></script>

链接到head标签之间。 谁知道解决方案?

2 个答案:

答案 0 :(得分:2)

试试这个:

$head_pos = strpos($html, '</head>'); // find position of the </head>
$newHtml = substr($html, 0, $head_pos) . $stringToAdd . substr($html, $head_pos);

答案 1 :(得分:2)

请使用strpos()和substr()查找位置,然后添加。