2011-01-21 3 views
3

simplehtmldom.sourceforge.net을 사용하여 HTML 코드를 조작하려고합니다. 이것은 내가 지금까지 가지고있다. 새 파일을 만들거나 index.htmlindex.php으로 설정하고 index.html에서 head 태그를 복사 할 수 있습니다. 문제는 어떻게 링크 태그를 삽입 할 수 있습니까?SimpleHtmlDom을 사용하여 HTML의 헤드 태그간에 링크 태그를 삽입하는 방법

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

헤드 태그 사이에? 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> 

을 :이처럼 사용할 수 있습니다

// Append a element 
$e->outertext = $e->outertext . '<div>foo<div>'; 

(클래스에 따라) 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

관련 문제