DolDurma
DolDurma

Reputation: 17321

PHP get first child from div

In below HTML Code, I want to get the content of p tag

Example

<div class="body" style="text-align: justify;padding: 10px;">
   <p style="text-align: justify;" dir="RTL">
      ...............
   </p>

   TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
</div>

My current code is :

$dom = new DOMDocument;
$dom->loadHTML($content);

echo $dom->getElementsByTagName('div')->item(0)->childNodes;

Why this code isn't correct and how to resolve this issue.

Upvotes: 1

Views: 10844

Answers (1)

ElefantPhace
ElefantPhace

Reputation: 3814

Interesting:

If you have this:

<?php
$content ='
<div class="body" style="text-align: justify;padding: 10px;">
    <p>this is a paragraph</p>
    TEST TEST TEST TEST 
</div>';

$dom = new DOMDocument;
$dom->loadHTML($content);

$firstChildOfDiv = $dom->getElementsByTagName('div')->item(0)->childNodes->item(0);
$value = $firstChildOfDiv->nodeValue;
echo "<PRE>",var_dump($value),"</PRE>";

Your output will be:

string(1) " "

However, if you have this:

<?php
$content ='
<div class="body" style="text-align: justify;padding: 10px;"><p>this is a paragraph</p> TEST TEST TEST TEST </div>';

$dom = new DOMDocument;
$dom->loadHTML($content);

$firstChildOfDiv = $dom->getElementsByTagName('div')->item(0)->childNodes->item(0);
$value = $firstChildOfDiv->nodeValue;
echo "<PRE>",var_dump($value),"</PRE>";

Your output will be as expected:

 string(19) "this is a paragraph"

Seems like something weird is happening with the whitespace

Upvotes: 5

Related Questions