php : parse html : extract script tags from body and inject before </body>?

倖福魔咒の 提交于 2019-12-03 23:09:57
$js = "";
$content = file_get_contents("http://website.com");
preg_match_all('#<script(.*?)</script>#is', $content, $matches);
foreach ($matches[0] as $value) {
    $js .= $value;
}
$content = preg_replace('#<script(.*?)</script>#is', '', $content); 
echo $content = preg_replace('#<body(.*?)</body>#is', '<body$1'.$js.'</body>', $content);

To select all script nodes with a src-attribute

$xpathWithSrc = '//script[@src]';

To select all script nodes with content:

$xpathWithBody = '//script[string-length(text()) > 1]';

Basic usage(Replace the query with your actual xpath-query):

$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXpath($doc);

foreach($xpath->query('//body//script[string-length(text()) > 1]') as $queryResult) {
    // access the element here. Documentation:
    // http://www.php.net/manual/de/class.domelement.php
}

Try https://github.com/fabpot/goutte it's intuitive and easy to use.

If you're really looking for an easy lib for this, I can recommend this one:

$dom = str_get_html($html);
$scripts = $dom->find('script')->remove;
$dom->find('body', 0)->after($scripts);
echo $dom;

There's really no easier way to do things like this in PHP.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!