PHP DOM and returning only part of document

2019-08-14 04:06发布

I Just discovered domdocument and had previously been using regex..

I need to return the entire form element with all the inputs.

I don't need to create an entire document i just want that part, in a string that I can manipulate. I have been messing with the following chunk of code trying to make it do something useful, but so far, nothing.

Can anyone make sense of this before I go back to regex?

//get HTML into variable
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.openroadlending.com/Apply.aspx?aid=134');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$html=curl_exec($curl);

$dom = new domDocument;
@$dom->loadHTML($html);
$dom->preserveWhiteSpace=false;

$xpath = new DOMXPath($dom);
$body = $xpath->query('html/');

echo var_dump($body);

//echo $body->item(0);
$inputs = $xpath->getElementsByTagName('form');

// foreach($inputs as $in){
// $input = $in->saveHTML;
// //echo $input;
// }

2条回答
男人必须洒脱
2楼-- · 2019-08-14 05:00

DOMXPath has no method getElementsByTagName. You can get to the forms via several methods

  1. getElementsByTagName

    $forms = $dom->getElementsByTagName('form');
    
  2. XPath query

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

Once you have the form you're after (by selecting it from the $forms collection or by using a more specialised XPath query), you can get the HTML as a string using

$formHTML = $dom->saveHTML($form);
查看更多
时光不老,我们不散
3楼-- · 2019-08-14 05:03

you can use this Function

function DOMinnerHTML($element) 
{ 
   $innerHTML = ""; 
   $children = $element->childNodes; 
   foreach ($children as $child) 
   { 
      $tmp_dom = new DOMDocument(); 
      $tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
      $innerHTML.=trim($tmp_dom->saveHTML()); 
   } 
   return $innerHTML; 
}

and use like this

$productspec=$dom->getElementsByTagName('form')
foreach($productspec as $data)
{ 
   echo DOMinnerHTML($data);
}

and You Can Use This function for Get Element By Class

function GetBYClass($Doc,$ClassName){
    $finder = new DomXPath($Doc);
    return($finder->query("//*[contains(@class, '$ClassName')]"));
} 

and This function Is Not Related To This Question But It Useful

function ExtractText($node) {
     if($node==NULL)return false;    
     if (XML_TEXT_NODE === $node->nodeType || XML_CDATA_SECTION_NODE === $node->nodeType) {
         return $node->nodeValue;
     } else if (XML_ELEMENT_NODE === $node->nodeType || XML_DOCUMENT_NODE === $node->nodeType || XML_DOCUMENT_FRAG_NODE === $node->nodeType) {
       if ('script' === $node->nodeName) return '';

     $text = '';
     foreach($node->childNodes as $childNode) {
        $text .= $this->extractText($childNode);
     }
     return $text;
     }
}
查看更多
登录 后发表回答