如何分割php xpath查询的结果

如何分割php xpath查询的结果,php,foreach,Php,Foreach,我正在尝试构建一个页面,当一个网站URL被添加到输入中时,我的php将删除该页面,并在该页面上显示每个表单的输入名称 我已经成功地做到了这一点,但是我试图分割结果,以便在一个页面上有多个表单时更容易阅读 <form action="" method="post"> <label style="color:#000000; font-family:arial, helvetica, sans-serif; font-size:16px; displ

我正在尝试构建一个页面,当一个网站URL被添加到输入中时,我的php将删除该页面,并在该页面上显示每个表单的输入名称

我已经成功地做到了这一点,但是我试图分割结果,以便在一个页面上有多个表单时更容易阅读

<form action="" method="post">
                <label style="color:#000000; font-family:arial, helvetica, sans-serif; font-size:16px; display:block;">Website URL:</label><br>
                <input type="text" name="website-url-value" id="website-url-value" style="border:1px solid #000;" />
                <div style="display:block; clear:both; margin-bottom:20px;"></div>
                <input type="submit" name="submit" value="Find forms" />
            </form>

        <?php
            $html = file_get_contents($_POST['website-url-value']);
            $website_doc = new DOMDocument();
            libxml_use_internal_errors(TRUE); //disable libxml errors
            if(!empty($html)){ //if any html is actually returned
                $website_doc->loadHTML($html);
                libxml_clear_errors(); //remove errors for bad html

                $website_xpath = new DOMXPath($website_doc);
                $form_total = 1; // initial form counter
                //get all the form fields
                $full_forms = $website_xpath->query('
                    //form
                '); // find forms on page
                $full_inputs = $website_xpath->query('
                    //input[@type="text"]|
                    //input[@type="radio"]|
                    //input[@type="checkbox"]|
                    //input[@type="tel"]|
                    //input[@type="email"]|
                    //input[@type="date"]|
                    //input[@type="number"]|
                    //input[@type="time"]|
                    //textarea|
                    //select'
                ); // find form fields with these types
                if($full_inputs->length > 0){
                    foreach($full_inputs as $single_input){
                        echo $single_input->getAttribute('name') . '<br />'; // show each field followed by new line
                    }
                }
                if($full_forms->length > 0){
                    foreach($full_forms as $single_form){
                        echo '<strong>' . $single_form->nodeName . " " . $form_total++ . '</strong><br />'; // show form plus count
                    }
                }
            }
        ?>

网站URL:
loadHTML($html); libxml_clear_errors()//删除错误html的错误 $website\u xpath=新的DOMXPath($website\u doc); $form_total=1;//首字母计数器 //获取所有表单字段 $full\u forms=$website\u xpath->query(' //形式 '); // 在第页上查找表单 $full\u inputs=$website\u xpath->query(' //输入[@type=“text”]| //输入[@type=“radio”]| //输入[@type=“checkbox”]| //输入[@type=“tel”]| //输入[@type=“email”]| //输入[@type=“date”]| //输入[@type=“number”]| //输入[@type=“time”]| //文本区| //选择' ); // 查找具有这些类型的表单字段 如果($full_inputs->长度>0){ foreach($完整输入为$单一输入){ echo$single_input->getAttribute('name')。
;//显示每个字段,后跟新行 } } 如果($full_forms->length>0){ foreach($full_forms作为$single_form){ 回显“”.$single\u form->nodeName.“$form\u total++.
”//显示表单加计数 } } } ?>
我预计结果如下: 表格1: 名字 姓氏 电子邮件

表格2: 名字 姓氏 电话

但目前我得到的结果如下:

名字 姓氏 电子邮件 名字 姓氏 电话 表格1:
表单2:

您要做的是从html文档中获取所有输入,您需要做的是一次获取一个表单并获取它们的相关输入

还有一件事是xpath返回nodelist作为结果,但我们可以使用nodelist并将其再次转换为xpath以进一步查询。为此,您可以使用
substant
参数并将节点列表作为第二个参数传递

试试这个:

if(!empty($html)) {
    $website_doc = new DOMDocument();

    libxml_use_internal_errors(TRUE); //disable libxml errors

    $website_doc->loadHTML($html);

    libxml_clear_errors(); //remove errors for bad html

    $xpath = new DOMXPath($website_doc);

    $forms = $xpath->query("//form");

    foreach($forms as $key => $form) {
        $inputs = $xpath->query('descendant::
            input[@type="text"]|
            input[@type="radio"]|
            input[@type="checkbox"]|
            input[@type="tel"]|
            input[@type="email"]|
            input[@type="date"]|
            input[@type="number"]|
            input[@type="time"]|
            textarea|
            select', $form);

        echo "Form ".($key+1)." <br>";

        foreach ($inputs as $input) {
            echo $input->getAttribute('name') . '<br />';
        }

        echo "<br>";
    }
}
if(!empty($html)){
$website_doc=新的DOMDocument();
libxml\u使用内部错误(TRUE);//禁用libxml错误
$website\u doc->loadHTML($html);
libxml_clear_errors();//删除错误html的错误
$xpath=newdomxpath($website\u doc);
$forms=$xpath->query(“//form”);
foreach($key=>$form形式的表单){
$inputs=$xpath->query('子体::
输入[@type=“text”]|
输入[@type=“radio”]|
输入[@type=“checkbox”]|
输入[@type=“tel”]|
输入[@type=“email”]|
输入[@type=“date”]|
输入[@type=“number”]|
输入[@type=“time”]|
文本区|
选择“,$form);
回显“表单”。($key+1)。“
”; foreach($inputs作为$input){ echo$input->getAttribute('name')。
; } 回声“
”; } }
感谢您的帮助,它现在可以拆分结果,但不再显示页面上的所有表单,而是只显示第一个表单,但会重复每个表单计数。例如:Form1:FirstName LastName电子邮件form2:FirstName LastName电子邮件您正在搜索它正在显示第一个表单,但稍后您将显示它的显示
Form1:。。。form2:…
这意味着它将获得多个表单,它应该列出html中的所有表单。发布您所有的html格式,以便检查对不起,您是正确的!它显示的是两个表单中的字段,仔细看后,它似乎只显示文本类型的字段。我通过在每个输入类型行的开头添加后代::来实现这一点。谢谢你的帮助,它让我到达了我需要的地方!!!很高兴听到它为您解决了,它应该在不向每个项目添加后代的情况下工作,但是,好吧,享受吧