",AFTER,EXCL" /> ",AFTER,EXCL" /> ",AFTER,EXCL"/>

How to remove unseen style element from html tag

49 Views Asked by At

I am webscraping a website and I am having difficulty with one item.

$coprp = split_string($coprp, "<td id=\"p\" class=\"datatable-item\">",AFTER,EXCL);
$coprp = split_string($coprp, "td id=\"nch\"", BEFORE, EXCL);
$coprp = strip_tags($coprp);
$coprp = trim($coprp);
$coprp= str_split($coprp,100);
$commd[2] = $coprp[0];

the target line is:

td id="p" class="datatable-item" style="background-color: rgb(255, 255, 255);">3.7624</td

The problem is I can get the 3.7624 and display it but I can not put it into an array for numbers because it has this style elementattached that is unseen in the regular html.

Is there a simple direct way to remove this unseen line using PHP?

1

There are 1 best solutions below

0
Ken Lee On

If I understand your question correctly, your tag should be:

<td id="p" class="datatable-item" style="background-color: rgb(255, 255, 255);">3.7624</td>

In PHP, to get the data from a td tag, you can use DOMDocument

This is a working code:

<?php
$html='<td id="p" class="datatable-item" style="background-color: rgb(255, 255, 255);">3.7624</td>';
$dom = new DOMDocument();
$dom->loadHtml($html);
$x = new DOMXpath($dom);
foreach($x->query('//td') as $td){

    echo $td->textContent;

}

On execution you will be 3.7624 (without style element)