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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何使用SimpleHtmlDom在HTML标题上的头标记之间插入链接标记脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图通过使用 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");
}

解决方法

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;

但是你明白了,对吗?

本图文内容来网友网络收集整理提供,作为学习参考使用,版权属于原作者。

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何使用SimpleHtmlDom在HTML标题上的头标记之间插入链接标记全部内容,希望文章能够帮你解决php – 如何使用SimpleHtmlDom在HTML标题上的头标记之间插入链接标记所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。