繁体   English   中英

从PHP简单HTML DOM解析器中删除html标签

[英]Delete html tags from PHP Simple HTML DOM Parser

当我想从此代码获取外部数据(例如作者名或网站名)时,我想从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>

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

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

这是simpleHtmlDomParser的文档

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

这里有一个表,我要删除此td <td colspan="5">所有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?

以我为例,我正在抓一张桌子,需要卸下脚踏板。 像这样:

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的单元并删除其内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM