PHP DOMDocument:表单元素的完整转换?包装和去除? [英] php DOMDocument: complete transform of form element? wrapping and removing?

查看:30
本文介绍了PHP DOMDocument:表单元素的完整转换?包装和去除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,
$ form 拥有这个...

hey guys, $form holds this...

<form method="post" action="">
    <input type="hidden" name="ip" value="127.0.0.1">
    <label for="s2email">Your Email</label>
    <input type="text" name="email" id="s2email" value="email..." size="20" onfocus="if (this.value == 'email...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'email...';}">
    <input type="submit" name="subscribe" value="Subscribe">
    <input type="submit" name="unsubscribe" value="Unsubscribe">
</form>

由于我不知道 DOMDocument 实际上,我需要在上面的$ form上进行两件事转换。

Since I have no idea what's possible with the DOMDocument class I need to ask for help. I actually have two things I need to transform on the $form above.

1。) label-s2email及其输入# sd-email 应该包装在< div class = name>
中(通过id或for-attribute)并将这两个文件包装成div?

1.) label-s2email and its input#sd-email should be wrapped inside a <div class="name"> Is that even possible to select those two (via id or for-attribute) and wrap those two into a div?

2。)是否可以用php删除onfocus和onblur属性?

2.) Is it possible to remove the onfocus and onblur attribute with php?

感谢您的帮助

推荐答案

由于找不到合适的副本显示了如何使用 DOM 包装节点,这是解决方案:

Since I cannot find a suitable duplicate that shows how to wrap nodes with DOM, here is the solution:

// Setup DOMDocument and XPath
$dom = new DOMDocument;
$dom->loadHTML($form);
$xpath = new DOMXPath($dom);

// Create <DIV> with class attribute name
$div = $dom->createElement('div');
$div->setAttribute('class', 'name');

// Find <input id="s2email"> and remove event attributes
$input = $xpath->query('/html/body/form/input[@id="s2email"]')->item(0);
$input->removeAttribute('onfocus');
$input->removeAttribute('onblur');

// Find <label for="s2email"> and insert new DIV before that
$label = $xpath->query('/html/body/form/label[@for="s2email"]')->item(0);
$label->parentNode->insertBefore($div, $label);

// Move <label> and <input> into the new <div>
$div->appendChild($label);
$div->appendChild($input);

// Echo the <form> outer HTML
echo $dom->saveHTML($dom->getElementsByTagName('form')->item(0));

上面的代码将产生(实时演示):

The above code will produce (live demo):

<form method="post" action="">
    <input type="hidden" name="ip" value="127.0.0.1"><div class="name">
<label for="s2email">Your Email</label><input type="text" name="email" id="s2email" value="email..." size="20">
</div>
    <input type="submit" name="subscribe" value="Subscribe"><input type="submit" name="unsubscribe" value="Unsubscribe">
</form>

请注意,为了将节点传递给 saveHTML ,您需要PHP 5.3.6。参见

Note that in order to pass a node to saveHTML, you need PHP 5.3.6. See

  • http://gooh.posterous.com/the-dom-goodie-in-php-536
  • How to return outer html of DOMDocument?

在此之前可能的解决方法。

for possible workarounds before that.

这篇关于PHP DOMDocument:表单元素的完整转换?包装和去除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆