2015-01-16 61 views
1

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

一个特定的输入需要由<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>'; 
    } 
} 

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

+1

您获取dom元素的语法是错误的。它应该是'$ dom->的getElementsByTagName(“变量名”)' – Ankit

回答

0

这里有几件事情错了你的语法

$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"); 

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

为了让每个输入字段的父节点,你可以在一个DIV使用

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

为组中的每个表,尝试这样做:

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

,并在结束

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

如果你想在现有的div中插入整个表单,你不能用PHP来做到这一点。因为一旦HTML被重新调整,就不能使用PHP插入HTML。

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

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

感谢,代码已经与语法错误的工作,我不知道如何再次 – Snowball

+0

感谢插入在特定情况下,DIV,但我只是想给一个组将表单的特定输入字段转换为div。我需要创建一个if语句来检查给定输入字段的父元素是否具有这些样式属性,或者输入字段的名称是否类似于“b_7e0d9965bedeea1cc5f7217a9_4685998a30” – Snowball

+1

您是否尝试过使用$ domElement-> parentNode来获取' input'场的父节点.. – Ankit

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 '>'; 
} 
+0

感谢您的想法@Ankit使用parentNode – Snowball

相关问题