Fetching First value from nodeValue using DOMDocument in PHP
Posted: 03 Apr 2015, 14:02
Hi,
I have the following code that uses DOMDocument to get some data from a html string in PHP.
- I have to get the first part from that div, the value: "Nr. 123", but it is fetching the whole nodeValue of the Div class "dv1".
How can I get the first string from that Div, without the other texts in the whole nodeValue?
Thanks.
I have the following code that uses DOMDocument to get some data from a html string in PHP.
Code: Select all
// string with HTML content
$strhtml = '<div class="dv1">Nr. 123
<div>Website: <span> https://coursesweb.net/ </span></div>
<div>';
// create the DOMDocument object, and load HTML from string
$dom = new DOMDocument();
$dom->loadHTML($strhtml);
$data = '';
// get data from the Div with class 'dv1'
foreach($dom->getElementsByTagName('div') as $el) {
if($el->getAttribute('class') == 'dv1') {
$data = $el->nodeValue;
}
}
echo $data; // Nr. 123 Website: https://coursesweb.net/
How can I get the first string from that Div, without the other texts in the whole nodeValue?
Thanks.