2011-01-21 6 views
3

simplehtmldom.sourceforge.netを使用してHTMLコードを操作しようとしています。これは私がこれまでに持っているものです。新しいファイルを作成するか、またはをindex.htmlindex.phpに設定し、index.htmlからheadタグをコピーします。問題は、リンクタグを挿入するにはどうすればいいのですか?SimpleHtmlDomを使用してHTMLのheadタグ間にリンクタグを挿入する方法

<link href="style.css" rel="stylesheet" type="text/css" /> 

headタグの間に問題がありますか?

あなたはこのように使用することができます
// Append a element 
$e->outertext = $e->outertext . '<div>foo<div>'; 

::documentation(:HTML要素の属性にアクセスする方法/ヒント?節)から

<?php 
# create and load the HTML 
include('simple_html_dom.php'); 
// get DOM from URL or file 
$html = file_get_html('D:\xampp\htdocs\solofile\index.html'); 
$indexFile ='index.php'; 
$openIndexFile = fopen($indexFile,'a'); 
//find head tag 
foreach($html->find('head') as $e) 
{ 
$findHead = $e->outertext; 
fwrite($openIndexFile, "\n" .$findHead. "\n"); 
} 

答えて

7

$e = $html->find('head')->innertext; // Should take all HTML inside <head></head> w/o <head></head 
$e = $e.'<link href="style.css" rel="stylesheet" type="text/css" />'; // inserting the CSS at the end of what's inside <head></head> 

私はしませんでした(クラスに応じて)2行目にする必要があるかもしれません。

$f = $html->find('head'); 
$e = $f->innertext; 

しかし、あなたはそのアイデアを得ていますよね? ;)

+0

+1のスプーン給餌ではない – Gordon

+0

OoopsのすべてのHTML内部のように?だから、すべてのHTMLタグが削除されますか?それは私が気にしていることではない。私はちょうど$ e行を挿入するからです。 タグ内のHTML行全体を削除することはできません。 – woninana

+0

@ Stupefy101: '//内側のものにCSSを挿入する'は、すべてのhtmlの後に 'link'を追加することを意味します。そこには何も削除してはいけません(もしsimplehtmldomが正しく行われていれば)。 – Shikiryu