PHP简单HTML DOM解析器:我想用PHP简单DOM解析器将javascript链接添加到HTML DOM


PHP Simple HTML DOM Parser: I want to add javascript link to html dom with php simple dom parser

我使用PHP Simple HTML DOM Parser,我想向HTML DOM添加javascript链接。这是我的密码。

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

我想添加

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

头标签之间的链接。谁知道解决方案?

试试这个:

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

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