Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP在dom中移动标记/元素_Php_Jquery_Html_Dom - Fatal编程技术网

使用PHP在dom中移动标记/元素

使用PHP在dom中移动标记/元素,php,jquery,html,dom,Php,Jquery,Html,Dom,我有一个问题,我正在用php编写我自己的helper类,但我想在DOM(文档)中移动标记/元素,例如,我想在头部移动链接标记,在主体底部移动js (类似于jquery中的$(“link”).appendTo(“head”).remove();) 我怎样才能继续? 我知道PHP中存在DOMDocument API, 如果可能的话,我想举个例子 谢谢你的帮助 (对不起,我的英语不好,我是法国人……你可以使用,要做到这一点,我稍后会制作一个块系统,因为它太复杂了。 简单地说,我这样做是为了链接标签:

我有一个问题,我正在用php编写我自己的helper类,但我想在DOM(文档)中移动标记/元素,例如,我想在头部移动链接标记,在主体底部移动js

(类似于jquery中的$(“link”).appendTo(“head”).remove();)

我怎样才能继续? 我知道PHP中存在DOMDocument API, 如果可能的话,我想举个例子

谢谢你的帮助


(对不起,我的英语不好,我是法国人……

你可以使用,要做到这一点,我稍后会制作一个块系统,因为它太复杂了。 简单地说,我这样做是为了链接标签:

/* 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一样抓取HTML5和XML文档

你可以找到它

它应该允许你做你想做的事情

示例用法:

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>');

[...]
名称空间PowerTools;
//获取文件内容
$htmlcode=文件\u获取\u内容('https://github.com' );
//根据文件字符串定义您的DOMCrawler
$H=新的DOM\U查询($htmlcode);
//基于现有的DOM_查询实例定义您的DOMCrawler
$H=新的DOM_查询($H->select('body');
//传递字符串(CSS选择器)
$s=$H->select('div.foo');
//传递元素对象(DOM元素)
$s=$H->select($documentBody);
//传递DOM查询对象
$s=$H->select($H->select('p+p'));
//选择主体标记
$body=$H->select('body');
//将不同的类组合为一个选择器,以获取所有站点块
$siteblocks=$body->select('.site header、.mashead、.site body、.site footer');
//像使用jQuery一样嵌套方法
$siteblocks->select('button')->add('span')->addClass('icon-icon-printer');
//使用lambda函数设置所有站点块的文本
$siteblocks->text(函数($i,$val){
返回$i。“-”$val->attr('class');
});
//将以下HTML附加到所有站点块
$siteblocks->append(“”);
//使用子体选择器选择站点的页脚
$sitefooter=$body->select('.site footer>.site center');
//设置网站页脚的一些属性
$sitefooter->attr(数组('id'=>'aweesome','data val'=>'see');
//使用lambda函数设置所有场地块的属性
$siteblocks->attr('data-val',函数($i,$val){
返回$i。“-”$val->attr('class')。“-凯利·克拉克摄”;
});
//选择网站页脚的父项
$sitefooterparent=$sitefooter->parent();
//删除站点页脚父级中所有i标记的类
$sitefooterparent->select('i')->removeAttr('class');
//将站点的页脚包装在两个nex选择器中
$sitefooter->wrap(“”);
[...]

支持的方法:
  • [x] (一)
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x] (二)
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]
  • [x]

  • 更名为“选择”,原因显而易见
  • 重命名为“void”,因为“empty”在PHP中是保留字

  • 谢谢。只是,我也想用一个函数setblock(name)创建一个“块系统”,我可以创建一个伪标记(like)并用regex替换,在一个循环中使用DOMNode::appendChild()方法吗?那行吗?试着写一些代码,这样我们就可以检查并告诉你什么是好的,什么是错的。此外-如果答案有效,你应该投票/接受它,让其他人知道它是有用的。谢谢,我正在询问,这是有用的!但当一个称为“块系统”时,它意味着什么时候设置了一个函数setblock(“jstobottom”);以及函数脚本(“jquery”,数组(“block”=>“jstobottom”);我想替换dom中的script()函数,其中是setblock()函数。我似乎在cakephp框架(带有fetch(block)的Helper类)中看到了这一点