加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何使用SimpleHtmlDom在HTML标题上的头标记之间插入链接

发布时间:2020-12-13 21:40:01 所属栏目:PHP教程 来源:网络整理
导读:我试图通过使用 simplehtmldom.sourceforge.net来操纵HTML代码.这是我到目前为止.我可以创建一个新文件或将index.html转换为index.php并从index.html复制head标签.问题是,我怎么能插入链接标签: link href="style.css" rel="stylesheet" type="text/css" /
我试图通过使用 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:xampphtdocssolofileindex.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");
}

解决方法

从 documentation(部分:如何访问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;

但是你明白了,对吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读