簡體   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