Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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文档HTML操作_Php_Html_Domdocument - Fatal编程技术网

PHP文档HTML操作

PHP文档HTML操作,php,html,domdocument,Php,Html,Domdocument,我有下面的HTML <div id="container"> <div id="current">Current Div</div> </div> HTML问题 <div id="container"> <div id="current"> <div id="new">New Div</div> Current Div </div>

我有下面的HTML

<div id="container">
    <div id="current">Current Div</div>
</div>
HTML问题

<div id="container">
    <div id="current">
        <div id="new">New Div</div>
        Current Div
    </div>
</div>
HTML

您可以使用:

这是基于以下HTML的。注意
表单
元素有一个id:

<div id="container">
    <form id="form" method="post" action="">
        <input type="text" name="username" id="username">
    </form>
</div>
编辑#2

我差点忘了XPath。。。如果您想变得更加简洁/富有冒险精神,可以使用查找DOM节点:

$xpath = new DOMXPath($doc);
$form  = $xpath->query('body/div[@id="container"]/form')->item(0);
$input = $xpath->query('body//input[@id="username"]')->item(0);
XPath查询语法非常隐晦,但功能强大,我不能说我是专家,所以我不能谈论这些查询的效率。此外,您可能还需要添加错误检查--
query
方法返回具有
length
属性的
DOMNodeList


最后值得注意的是,
DOMDocument
DOCTYPE
HTML
body
标记装饰HTML片段;这就是XPath查询从
body

遍历的原因,为什么脚本在引用输入文本框的id而不是id为“current”的div时会中断有什么区别?嗨,里奇。它坏了?有趣的。。。您是否遇到了特定的错误?以下内容对我有用:
$input=$doc->createElement('input')$输入->设置属性('type','text')$输入->设置属性('id','foo')$输入->设置属性('value','bar')$容器->插入之前($input,$current)我已经更新了我原来的帖子,包括更多的细节:)你知道。。。我想我看错了你的评论,
input
元素是否已经存在于
index.html
中?如果是这样的话,你能发布导致问题的PHP行吗?不用担心,html已经包含了输入元素。我已经在我的原始帖子中粘贴了php、html和错误。它似乎不喜欢insertBefore方法?
$doc = new DOMDocument();
$doc->formatOutput = true;

$doc->loadHTMLFile('index.html');
$container = $doc->getElementById('container');
$current = $doc->getElementById('username');
$new = $doc->createElement('div', 'New Div');

$new->setAttribute('id', 'new');
$container->insertBefore($new, $current);
echo $doc->saveHTML();
<div id="container">
    <form method="post" action="">
        <input type="text" name="username" id="username" />
    </form>
</div>
Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error' 
in index.php:55 Stack trace: #0 index.php(55): 
DOMNode->insertBefore(Object(DOMElement), Object(DOMElement))
<?php

$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadHTMLFile('index.html');

$container = $doc->getElementById('container');
$current = $doc->getElementById('current');
$new = $doc->createElement('div', 'New Div');

$new->setAttribute('id', 'new');
$container->insertBefore($new, $current);

var_dump($doc->saveHTML());
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
    <body>
        <div id="container">
            <div id="new">New Div</div>
            <div id="current">Current Div</div>
        </div>
    </body>
</html>
$form = $doc->getElementById('form');
$username = $doc->getElementById('username');
$username->setAttribute('value', 'Enter username!');

var_dump($doc->saveHTML());
<div id="container">
    <form id="form" method="post" action="">
        <input type="text" name="username" id="username">
    </form>
</div>
// find div#container element
$container = $doc->getElementBy('id');
// find all form elements that are children of div#container
$forms = $container->getElementsByTagName('form');

if (0 !== $forms->length) {
   $form = $forms->item(0);
}

// etc.
$xpath = new DOMXPath($doc);
$form  = $xpath->query('body/div[@id="container"]/form')->item(0);
$input = $xpath->query('body//input[@id="username"]')->item(0);