簡體   English   中英

如何使用SimpleHtmlDom在HTML標題上的頭標記之間插入鏈接標記

[英]How to insert link tags between head tags on HTML using SimpleHtmlDom

我試圖通過使用simplehtmldom.sourceforge.net來操縱HTML代碼。 這是我到目前為止。 我可以創建一個新文件或將index.html轉換index.php並從index.html復制head標簽。 問題是,我怎么能插入鏈接標簽:

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

頭部標簽之間?

<?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");
}

文檔 (部分:如何訪問HTML元素的屬性?/提示):

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

您可以這樣使用:

$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;

但是你明白了,對嗎? ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM