Move tags / elements to dom using PHP

I have a problem, I am coding my own helper class in php, but I want to move tags / elements in the DOM (document), for example, I want to move the link tag in head and js from the bottom to the body ...

(do like $ ("link"). appendTo ("head"). remove (); in jquery)

How can I proceed? I know there is a DOMDocument API in PHP, If possible, I would like to give an example.

Thanks for the help.

(sorry for my bad english, i'm french ...)

+3


source to share


3 answers


I've created a library that allows you to traverse HTML5 and XML documents in the same way as with jQuery.

You can find it here .

This should allow you to do exactly what you want!

Usage example:

namespace PowerTools;

// Get file content
$htmlcode = file_get_contents( 'https://github.com' );

// Define your DOMCrawler based on file string
$H = new DOM_Query( $htmlcode );

// Define your DOMCrawler based on an existing DOM_Query instance
$H = new DOM_Query( $H->select('body') );

// Passing a string (CSS selector)
$s = $H->select( 'div.foo' );

// Passing an element object (DOM Element)
$s = $H->select( $documentBody );

// Passing a DOM Query object
$s = $H->select( $H->select('p + p') );

// Select the body tag
$body = $H->select('body');

// Combine different classes as one selector to get all site blocks
$siteblocks = $body->select('.site-header, .masthead, .site-body, .site-footer');

// Nest your methods just like you would with jQuery
$siteblocks->select('button')->add('span')->addClass('icon icon-printer');

// Use a lambda function to set the text of all site blocks
$siteblocks->text(function( $i, $val) {
    return $i . " - " . $val->attr('class');
});

// Append the following HTML to all site blocks
$siteblocks->append('<div class="site-center"></div>');

// Use a descendant selector to select the site footer
$sitefooter = $body->select('.site-footer > .site-center');

// Set some attributes for the site footer
$sitefooter->attr(array('id' => 'aweeesome', 'data-val' => 'see'));

// Use a lambda function to set the attributes of all site blocks
$siteblocks->attr('data-val', function( $i, $val) {
    return $i . " - " . $val->attr('class') . " - photo by Kelly Clark";
});

// Select the parent of the site footer
$sitefooterparent = $sitefooter->parent();

// Remove the class of all i-tags within the site footer parent
$sitefooterparent->select('i')->removeAttr('class');

// Wrap the site footer within two nex selectors
$sitefooter->wrap('<section><div class="footer-wrapper"></div></section>');

[...]

      




Supported methods:
  • Renamed 'select', for obvious reasons
  • Renamed 'void' since 'empty' is a reserved word in PHP
+2


source


You can use removeChild and appendChild to do this



0


source


I will make a block system later because it is too complex. I just did this for the link tag:

/* set view as html string */
ob_start();
require $view;
$html = ob_get_clean();

/* DOM */
$dom = new DOMDocument();
$dom->loadHTML($html);
$_head = $dom->getElementsByTagName("head")->item(0);
$links = $dom->getElementsByTagName("link");
foreach($links as $link) {
    $_head->appendChild($link);
}
$html = $dom->saveHTML();
/* render */
echo $html;

      

0


source







All Articles