0

I have divs with same Class value;

<div class="description">Div 1</div>
<div class="description">Div 2</div>
<div class="description">Div 3</div>

and I can parse and display them all with this code:

foreach($html->find('.description') as $icerik) { echo '<b>'.$icerik->plaintext.'</b>'; }

Now I want to display them individually. How to make them parse and display like first div, second div and third div?

Thanks.

4
  • Please provide required output Commented Dec 8, 2016 at 20:35
  • I'm not positive, but would $html->find('.description')[0]->plaintext work? It's referred to as array dereferencing implemented in 5.4. Commented Dec 8, 2016 at 20:38
  • Doesn't work. "Notice: Trying to get property of non-object in ..." Commented Dec 8, 2016 at 20:54
  • Ah, well the following works with DOMXPath (just tested): $html->query("//*[@class='description']")->item(0)->nodeValue; Commented Dec 8, 2016 at 21:00

2 Answers 2

1

Do you mean this?

foreach($html->find('.description') as $icerik) {
    echo '<div><b>'.$icerik->plaintext.'</b></div>';
}
Sign up to request clarification or add additional context in comments.

Comments

0

Since $html->find('.description') returns an array of dom node objects you might put the result in a variable in which you could access them directly.

$description_divs = $html->find('.description');
echo 'first div '.$description_divs[0]->plaintext.'<br><br>';
echo 'second div '.$description_divs[1]->plaintext.'<br><br>';
echo 'third div '.$description_divs[2]->plaintext.'<br><br>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.