将 tags/elements 移动到 dom 和 PHP
Move tags/elements in dom with PHP
我有问题,我正在 php 中编写自己的助手 class,但我想在 DOM(文档)中移动 tags/elements,例如,我想将 head 中的 link 标记和 js 移动到 body 中的底部...
(像 $("link").appendTo("head").remove(); 在 jquery)
我该如何进行?
我知道 PHP 中存在一个 DOMDocument API,
如果可以的话,我想要一个例子。
感谢您的帮助。
(抱歉我的英语不好,我是法国人...)
您可以使用 removeChild and appendChild 来做到这一点
以后会做一个方块系统,太复杂了。
简单地说,我为 link 标签做了这个:
/* 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;
我创建了一个库,允许您像抓取 jQuery 和 XML 文档一样抓取 HTML5。
你可以找到它here。
它应该能让你做你想做的事!
使用示例:
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's footer
$sitefooter = $body->select('.site-footer > .site-center');
// Set some attributes for the site's 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's footer
$sitefooterparent = $sitefooter->parent();
// Remove the class of all i-tags within the site's footer's parent
$sitefooterparent->select('i')->removeAttr('class');
// Wrap the site's footer within two nex selectors
$sitefooter->wrap('<section><div class="footer-wrapper"></div></section>');
[...]
支持的方法:
- [x] $ (1)
- [x]$.parseHTML
- [x]$.parseXML
- [x]$.parseJSON
- [x] $selection.add
- [x]$selection.addClass
- [x]$selection.after
- [x]$selection.append
- [x]$selection.attr
- [x]$selection.before
- [x]$selection.children
- [x]$selection.closest
- [x]$selection.contents
- [x]$selection.detach
- [x] $selection.each
- [x] $selection.eq
- [x] $selection.empty (2)
- [x]$selection.find
- [x]$selection.first
- [x]$selection.get
- [x]$selection.insertAfter
- [x]$selection.insertBefore
- [x]$selection.last
- [x] $selection.parent
- [x] $selection.parents
- [x]$selection.remove
- [x]$selection.removeAttr
- [x] $selection.removeClass
- [x]$selection.text
- [x]$selection.wrap
- 已重命名 'select',原因显而易见
- 重命名为'void',因为'empty'是PHP
中的保留字
我有问题,我正在 php 中编写自己的助手 class,但我想在 DOM(文档)中移动 tags/elements,例如,我想将 head 中的 link 标记和 js 移动到 body 中的底部...
(像 $("link").appendTo("head").remove(); 在 jquery)
我该如何进行? 我知道 PHP 中存在一个 DOMDocument API, 如果可以的话,我想要一个例子。
感谢您的帮助。
(抱歉我的英语不好,我是法国人...)
您可以使用 removeChild and appendChild 来做到这一点
以后会做一个方块系统,太复杂了。 简单地说,我为 link 标签做了这个:
/* 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;
我创建了一个库,允许您像抓取 jQuery 和 XML 文档一样抓取 HTML5。
你可以找到它here。
它应该能让你做你想做的事!
使用示例:
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's footer
$sitefooter = $body->select('.site-footer > .site-center');
// Set some attributes for the site's 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's footer
$sitefooterparent = $sitefooter->parent();
// Remove the class of all i-tags within the site's footer's parent
$sitefooterparent->select('i')->removeAttr('class');
// Wrap the site's footer within two nex selectors
$sitefooter->wrap('<section><div class="footer-wrapper"></div></section>');
[...]
支持的方法:
- [x] $ (1)
- [x]$.parseHTML
- [x]$.parseXML
- [x]$.parseJSON
- [x] $selection.add
- [x]$selection.addClass
- [x]$selection.after
- [x]$selection.append
- [x]$selection.attr
- [x]$selection.before
- [x]$selection.children
- [x]$selection.closest
- [x]$selection.contents
- [x]$selection.detach
- [x] $selection.each
- [x] $selection.eq
- [x] $selection.empty (2)
- [x]$selection.find
- [x]$selection.first
- [x]$selection.get
- [x]$selection.insertAfter
- [x]$selection.insertBefore
- [x]$selection.last
- [x] $selection.parent
- [x] $selection.parents
- [x]$selection.remove
- [x]$selection.removeAttr
- [x] $selection.removeClass
- [x]$selection.text
- [x]$selection.wrap
- 已重命名 'select',原因显而易见
- 重命名为'void',因为'empty'是PHP 中的保留字