简体   繁体   中英

Delete html tags from PHP Simple HTML DOM Parser

I wanna delete some words from simple_html_dom when get external data (eg name of Author or name of website ) from this code: `

<?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;

I don't know how can do it (delete flowing code from a table from url)

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

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

you can also delete directly from the dom the innertext between your TD

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

here is the doc for simpleHtmlDomParser

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

there is a table here I'm going to delete this td <td colspan="5"> all html files is here:

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

How to delete a td from a table in the simple_html_dom ?

In my case, I'm grabbing a table, and needed to remove the tfoot. Did so like:

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;

In your case, if you want to remove the whole row and you know the table row number, you can do something like:

$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

Where 'tr', 0 that would remove the first row, and 'tr', 3 would remove the fourth row.

Or even:

$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

That would get the first cell with colspan 5 and remove its contents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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