How to insert link tags between header tags in HTML using SimpleHtmlDom

I am trying to manipulate HTML codes using simplehtmldom.sourceforge.net . This is me so far. I could create a new file or turn index.html into index.php and copy the main tag from index.html. The problem is how can I insert link tags:

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

between title tags?

<?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");
}
+3
source share
1 answer

From the documentation (section: How to access HTML element attributes? / Tips):

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

What you can use as follows:

$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 inside <head></head>

I have not tried, but maybe (depending on the class) you may need to make the first line in 2.

$f = $html->find('head');
$e = $f->innertext;

But you get it, right ?;)

+7
source

Source: https://habr.com/ru/post/1786836/


All Articles