4

PHP シンプル HTML DOM パーサーを使用して、ホームページからの完全な html を持つ simple_html_dom オブジェクトのヘッド内に新しいスクリプト タガを追加することは可能ですか?

そのテンプレート内にいくつかのノードを追加する必要があります。このノードの 1 つは jquery を含むスクリプト タグであり、もう 1 つはデータベースから取得するテキストを含む div です。

私は以前に次のようなことをしました:( DOMDocument を使用)

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($remote);
$head = $dom->getElementsByTagName('head')->item(0);
$jquery = '$(document).ready(function(){ $("#feed_home").hide()});
  if (window.location.hash) {
    Destino = window.location.hash.replace(\'#!\', \'?\');

         window.location.href = window.location.href.split(\'#\')[0] + Destino;
}
';

$script = $dom->createElement('script', $jquery);
$script_type = $dom->createAttribute('type');

$script_type->value = 'application/javascript';
$script->appendChild($script_type);
$head->appendChild($script);
4

2 に答える 2

11

PHP シンプルな HTML DOM では次の操作が可能です。

$html = str_get_html($rawhtml);
$inject  = '<script type="text/javascript">alert("Hello")</script>';
$html->find('head', 0)->innertext = $inject.$html->find('head', 0)->innertext;
echo $html;

http://simplehtmldom.sourceforge.net/manual.htm#frag_access_tips

于 2013-08-12T12:33:09.217 に答える
1

いいえ、単純な html dom は dom 操作を行いません。phpquery を使用すると、次のことができます。

$doc->find('head')->append('<script src="foo"></script>');
于 2013-03-29T06:31:44.147 に答える