John Davidson

Php Dom getting nodeValue without stripping inline element tags

0 comments
Message:


Here is the sample html:


$html = '<table>
<tbody>
<tr>
<td><span style="background-color: #f1c40f;">Cell 1</span></td>
<td>Cell 2</td>
</tr>
</tbody>
</table>';


Target is to retrieve everything between "td" tags, including the "span" tag, if any. Here is the expected result:


<span style="background-color: #f1c40f;">Cell 1</span>
Cell 2

I tried to use DOMDocument()->getElementsByTagName("td")->saveHTML() and nodeValue without success.


Here is the php code I tried:


$html = '<table>
<tbody>
<tr>
<td><span style="background-color: #f1c40f;">Cell 1</span></td>
<td>Cell 2</td>
</tr>
</tbody>
</table>';
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $html);
$tds = $doc->getElementsByTagName("td");
foreach($tds as $td){
dump('saveHTML: '.$doc->saveHTML($td));
dump('nodeValue: '.$td->nodeValue);
}
die();

Here is the outputs:


^ "saveHTML: <td><span style="background-color: #f1c40f;">Cell 1</span></td>"
^ "nodeValue: Cell 1"
^ "saveHTML: <td>Cell 2</td>"
^ "nodeValue: Cell 2"

Did I do anything wrong? Thanks a lot!



My Answer:

This is a sample paragraph.


';
// Load the HTML string
$dom = new DOMDocument();
$dom->loadHTML($html);
// Get the

element
$p = $dom->getElementsByTagName('p')->item(0);
// Get the node value of the

element
$nodeValue = '';
foreach ($p->childNodes as $node) {
$nodeValue .= $dom->saveHTML($node);
}
echo $nodeValue; // Output: This is a sample paragraph.

Rate this post

4 of 5 based on 3561 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us