我试图通过使用来操作 HTML 代码simplehtmldom.sourceforge.net http://simplehtmldom.sourceforge.net。这是我到目前为止所得到的。我可以创建一个新文件或将索引.html to 索引.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");
}
来自文档 http://simplehtmldom.sourceforge.net/manual.htm(部分:如何访问 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;
但你明白了,对吧? ;)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)