从HTML中提取表单和输入字段

时间:2015-01-16 09:06:32

标签: php domdocument

我想从给定HTML的表单和输入字段中提取和回显属性值。我发现它可以用DOMDocument。

一个特定输入需要按<div style="position: absolute; left: -5000px;"></div>

分组

我认为我可以搜索输入的父元素是否具有此样式属性,或者输入字段的名称是否与b_7e0d9965bedeea1cc5f7217a9_4685998a30类似。但我不知道该怎么做。

这是代码:

$html = $theme['html']; // From a text input field
$dom = new DOMDocument();
if (@$dom->loadHTML($html)) {

    $forms = $dom->getelementsbytagname('form');

    foreach ($forms as $form) {

        $form_action        = $form->getAttribute('action');
        $form_method        = $form->getAttribute('method');
        $form_id                = $form->getAttribute('id');
        $form_name          = $form->getAttribute('name');
        $form_class         = $form->getAttribute('class');
        $form_target        = $form->getAttribute('target');

        echo '<form';
        if (!empty($form_action)) { echo ' action="'. $form_action .'"'; } 
        if (!empty($form_method)) { echo ' method="'. $form_method .'"'; }
        if (!empty($form_id)) { echo ' id="'. $form_id .'"'; }
        if (!empty($form_name)) { echo ' name="'. $form_name .'"'; }
        if (!empty($form_class)) { echo ' class="'. $form_class .'"'; }
        if (!empty($form_target)) { echo ' target="'. $form_target .'"'; } 
        echo '>';

        $inputs = $dom->getelementsbytagname('input');

        foreach ($inputs as $input) {

            $input_name     = $input->getAttribute('name');
            $input_value    = $input->getAttribute('value');
            $input_id           = $input->getAttribute('id');
            $input_type     = $input->getAttribute('type');
            $input_class    = $input->getAttribute('class');

            echo '<input';
            if (!empty($input_name)) { echo ' name="'. $input_name .'"'; } 
            if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
            if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
            if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
            if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
            echo '>';

        }

        echo '</form>';
    }
}

一些背景信息:我希望用户将其电子邮件表单代码复制并粘贴到文本框中。然后我想提取输入字段的属性,以便在我的模板中使用它们。

2 个答案:

答案 0 :(得分:1)

以下是您的语法错误

$html = $theme['html']; // Make sure $html is a String
$dom = new DOMDocument();
//if (@$dom->loadHTML($html)) {
if ($dom->loadHTML($html)) {  //remove @
  //$forms = $dom->getelementsbytagname('form');
   $forms = $dom->getElementsByTagName("form");

进行这些更改并再次检查。希望它应该工作

要获取每个输入字段的父节点,可以使用

 $parentOfInput = $input->parentNode();
 $parentAttribute = $parentOfInput->getAttribute('style');

要将每个表单分组到div中,请尝试执行以下操作:

  //echo '<form';
   echo '<div style="position: absolute; left: -5000px;"><form'

并在最后

 //echo '</form>';
  echo '</form></div>'    

如果要将整个表单插入现有div中,则无法使用PHP执行此操作。一旦HTML被重新连接,您就无法使用PHP插入HTML。

但是你可以使用javascript或在你的情况下使用AJAX。将您echo的整个HTML保存在变量中。然后在AJAX调用中传递该变量。

 $.ajax({url: "urFile.php"}).done(function( stringOfHTMLYouFormed ) {
    $("#divID").append(stringOfHTMLYouFormed );
 });

答案 1 :(得分:0)

我将$parent = $input->parentNode->getAttribute('style');添加到foreach循环中以查找Parent的样式。

之后我用它来测试$parent是否具有所需的样式来包装,然后将相应的输入字段放入div中。

$parent = $input->parentNode->getAttribute('style');
if ($parent == 'position: absolute; left: -5000px;') {
    echo '<div style="position: absolute; left: -5000px;">';
    echo '<input';
    if (!empty($input_name)) { echo ' name="'. $input_name .'"'; } 
    if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
    if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
    if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
    if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
    echo '></div>';
} else {            
    echo '<input';
    if (!empty($input_name)) { echo ' name="'. $input_name .'"'; } 
    if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
    if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
    if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
    if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
    echo '>';
}
相关问题