Search code examples
phphtmldomsimple-html-dom

How to use Simple HTML Dom to get text next to image element?


I am using Simple HTML Dom parser to get a text that next to img tag from an HTML string like:

HTML

<tr>                                  
  <td>
    <img alt="Checkbox checked, changed" src="/Images/crd_pgm_RedlineCheckSelected.gif">My TEXT HERE
  </td>   
</tr>

I tried writing the belo code:

<?php

  foreach($html->find('img') as $element) {
    $plantext = $element->next_sibling()->plaintext;
    echo $plantext;
  }

?>

But it shows NULL everytime. How can I scrape this text?


Solution

  • In the comments of simple_html_dom.php it states this text:

    Paperg add the text and plaintext to the selectors for the find syntax. plaintext implies text in the innertext of a node. text implies that the tag is a text node.

    When I add text to the selector for find I get the text

    foreach($html->find('img text') as $element) {
        echo $element->plaintext;
    }
    

    Output

    My TEXT HERE