将隐藏的输入捕获为字符串(使用PHP Simple HTML DOM Parser) [英] Grabbing hidden inputs as a string (Using PHP Simple HTML DOM Parser)

查看:83
本文介绍了将隐藏的输入捕获为字符串(使用PHP Simple HTML DOM Parser)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个具有4个输入,2个文本和2个隐藏状态的表单.我已经从名称中获取了两个文本输入值,分别是(get_me_two,get_me_three)和我也已经获取了表单操作,即(get_me.php).我现在想做的是获取2个隐藏的输入,而不是值.我想自己获取输入.

So I have a form that has 4 inputs, 2 text, 2 hidden. I've grabbed the two text input values from the name, which are (get_me_two, get_me_three) and I've also grabbed the form action which is (get_me.php). What I'm looking to do now is grab the 2 hidden inputs, but not the values. I want to grab the inputs themselves.

E.G:这是我的表格:

E.G: Here's my form:

<form action="get_me.php" method="post">
    <input type="text" name="get_me_two">
    <input type="text" name="get_me_three">
    <input type="hidden" name="meta_required" value="from">
    <input type="hidden" name="meta_forward_vars" value="0">
</form>

我想从这里获取的是两个隐藏的输入,不是值,是完整的字符串.

And what I want to grab from here is the two hidden inputs, Not the values, the complete string.

我不确定如何使用以下方法来获取这些信息:PHP简单HTML DOM解析器,如果有人知道一种很好的方式,如果没有,那么如果有一种替代方法也很好.掌握了这些内容后,我计划将2个输入值与隐藏的字符串一起传递给另一个页面,当然还有form动作.

I'm not sure how to grab these using: PHP Simple HTML DOM Parser, if anybody knows a way that would be great, if not, if there's an alternative that also would be great. Once I've grabbed these I plan on passing the 2 input values to another page with the hidden strings, and of course the form action.

此外,如果有人感兴趣,这是我的完整代码,其中包括简单的html dom功能.

Also, if anybody is interested here's my full code, which includes the simple html dom functionality.

<?php

include("simple_html_dom.php");

// Create DOM from URL or file
$html = file_get_html('form_show.php');
$html->load('
<form action="get_me.php" method="post">
<input type="text" name="get_me_two">
<input type="text" name="get_me_three">
<input type="hidden" name="meta_required" value="from">
<input type="hidden" name="meta_forward_vars" value="0">
</form>');

// Get the form action
foreach($html->find('form') as $element) 
   echo $element->action . '<br>';

// Get the input name       
foreach($html->find('input') as $element) 
   echo $element->name . '<br>';
?>

因此,最终结果将获取3个值,然后获取2个隐藏的输入(完整字符串).帮助将不胜感激,因为这使我有点发疯,试图完成此任务.

So, the end result would grab the 3 values, and then the 2 hidden inputs (full strings). Help would be much appreciated as It's driving me a little mad trying to get this done.

推荐答案

如果使用DomDocument,则可以执行以下操作:

If you use DomDocument, you could do the following:

<?php
    $hidden_inputs = array();
    $dom = new DOMDocument('1.0');
    @$dom->loadHTMLFile('form_show.php');

    // 1. get all inputs
    $nodes = $dom->getElementsByTagName('input');

    // 2. loop through elements
    foreach($nodes as $node) {
        if($node->hasAttributes()) {
            foreach($node->attributes as $attribute) {
                if($attribute->nodeName == 'type' && $attribute->nodeValue == 'hidden') {
                    $hidden_inputs[] = $node;
                }
            }
        }
    } unset($node);

    // 3. loop through hidden inputs and print HTML
    foreach($hidden_inputs as $node) {
        echo "<pre>" . htmlspecialchars($dom->saveHTML($node)) . "</pre>";
    } unset($node);

?>

这篇关于将隐藏的输入捕获为字符串(使用PHP Simple HTML DOM Parser)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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