dscc90150010 2013-02-02 08:55
浏览 59
已采纳

PHP DOM并只返回部分文档

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条回答 默认 最新

  • douqiao8032 2013-02-02 09:11
    关注

    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;
         }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图