【问题标题】:Delete html tags from PHP Simple HTML DOM Parser从 PHP Simple HTML DOM Parser 中删除 html 标签
【发布时间】:2013-12-26 15:56:22
【问题描述】:

当从这段代码中获取外部数据(例如作者姓名或网站名称)时,我想从 simple_html_dom 中删除一些单词:`

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

include('simple_html_dom.php');  
$html = new simple_html_dom();

// Create DOM from URL or file


$html = file_get_html('http://www.example.com');       

$myContent = $html->find('table', 0)->plaintext;
echo $myContent;

我不知道怎么做(从url的表中删除流动代码)

  <tr style="background: #ffd700;color:black;">

    <td colspan="5">**delete this words from table..**   
    </td></tr>

【问题讨论】:

标签: php html curl get html-parsing


【解决方案1】:

您也可以直接从 dom 中删除 TD 之间的内部文本

$html->find('table tr')->children(NUMBER OF THE TD TO EMPTY)->innertext = '';

这里是 simpleHtmlDomParser 的文档

http://simplehtmldom.sourceforge.net/manual.htm#section_traverse

【讨论】:

    【解决方案2】:

    在我的情况下,我正在抓一张桌子,需要移除 tfoot。喜欢:

    include("simple_html_dom.php");
    $html = str_get_html($curl_response_html); // load html from string
    $wtable = $html->find('table[id=sometableid]',0); // get table by id
    $wtable->find('tfoot',0)->outertext=''; // find the element in the table and remove it
    echo $wtable;
    

    在您的情况下,如果您想删除整行并且您知道表格行号,您可以执行以下操作:

    $wtable = $html->find('table[id=sometableid]',0); // get table by id
    $wtable->find('tr',0)->outertext=''; // find the element in the table and remove it
    

    'tr', 0 将删除第一行,'tr', 3 将删除第四行。

    甚至:

    $wtable = $html->find('table[id=sometableid]',0); // get table by id
    $wtable->find('td[colspan=5]',0)->innertext=''; // find the element and remove its contents
    

    这将获得第一个具有 colspan 5 的单元格并删除其内容。

    【讨论】:

      【解决方案3】:

      这里有一个表我要删除这个td&lt;td colspan="5"&gt;所有的html文件都在这里:

          <table cellspacing="6px" border="0px" cellpadding="0" align="center" width="670px" style="font-size:16pt;font-weight:bold;font-family:times new roman;margin-top:0px;border:1px solid #666666;text-align:center;">
      <tbody><tr><td colspan="4">text 1
      </td></tr><tr style="background: #ffd700;color:black;">
      
      <td colspan="5">text for delete‌   
      </td></tr><tr style="background: #fdfdad">
      <td colspan="5" style="font-size:13pt;">text2
      </td></tr><tr style="background: #ffffcc">
      <td colspan="2">text3
      </td><td>text4
      </td><td>text5
      </td></tr><tr style="background: #fdfdad">
      <td width="35px"><img src="PIC/PNG/UnitedStates-01.png" width="33" height="22">
      </td><td>text6
      </td><td>3015
      </td><td>2990
      </td></tr><tr style="background: #ffffcc">
      <td><img src="PIC/PNG/Europe-01.png" width="33" height="22">
      </td><td>text7
      </td><td>4100
      </td><td>4072
      </td></tr><tr style="background: #fdfdad">
      <td><img src="PIC/PNG/Canada-01.png" width="33" height="22">
      
      </td><td>2436
      </td><td>2366
      </td></tr></tbody></table>
      

      如何从 simple_html_dom 的表中删除一个 td ?

      【讨论】:

      • 以此为例 // 查找表标签PHP Simple HTML DOM Parser Manual $es = $html->find(''table td[align=center]') 中所有属性为align=center 的td 标签;你的例子需要这样的东西 $html->find('table tr td[colspan=5]')->innertext = '';
      猜你喜欢
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 2016-05-23
      • 2020-09-09
      相关资源
      最近更新 更多