Search code examples
phphtmldomsimple-html-dom

Extract HTML pure with PHP Simple HTML DOM Parser


Can you with "php simple html dom" take a piece of HTML and NOT only the content?

I have trie with:

foreach ($html->find('div.info-bar') as $infop) {
  $info = $infop->plaintext;
}

Unfortunately, the output is:

Level 24 Trophies 4201 Bronze 2725 Silver 1057 Gold 341 Platinum 78 Level 24 66 66 %

While I would like to extract the pure HTML..

This is my code:

include('simple_html_dom.php');
$html = file_get_html('https://my.playstation.com/obaidmiz04');
foreach ($html->find('div.info-bar') as $infop) {
  $info = $infop->plaintext;
}
echo $info;

Solution

  • $escapedHtmlChars = "";
    $htmlElements = "";
    $html = file_get_html('https://my.playstation.com/obaidmiz04');
    foreach ($html->find('div.info-bar') as $infop) {
      //You can see html characters in text
      //This shows you html codes, not for using
      $escapedHtmlChars .= htmlspecialchars($infop); 
      //You can see html elements
      $htmlElements .= $infop;
      //You can see only text in selected element
      $plainText .= $infop->plaintext;
    }
    echo $plainText;
    echo "<br /> <br />";
    echo $escapedHtmlChars;
    echo "<br /> <br />";
    echo $htmlElements;
    

    plaintext method returns only text in selected element.