0

I'm trying to parse this HTML from remote url using Simple HTML DOM Parser:

<div class="_5pbx userContent _3ds9 _3576" data-ft="data-ft='{"tn":"K"}'">
<p>text to be parsed</p>
<p>the rest of text</p>
</div>

The PHP snipe I used is as the following:

include (getdomain . "/lib/simple_html_dom.php");

$html = new simple_html_dom();
$get = file_get_contents("http://localhost/get/d.json");
$html->load($get);
foreach ($html->find('div[class=_5pbx userContent _3ds9 _3576]') as $link) {
 if(isset($link)){
          echo $link->plaintext ;
    }
  }

But it didn't work, I'm aware of the <p> tag and I tried to add if statement for that but still didn't work, as the following:

include (getdomain . "/lib/simple_html_dom.php");

$html = new simple_html_dom();
$get = file_get_contents("http://localhost/get/d.html");
$html->load($get);
foreach ($html->find('div[class=_5pbx userContent _3ds9 _3576]') as $link) {
 if(isset($link)){
foreach($link->find('p') as $tag)
    {
          echo $tag->plaintext ;
    }
  }
}

But all didn't work :(

Any idea?

3
  • Are you getting any PHP errors or warnings either displayed or logged? Also, in your include, is the getdomain a defined symbol in your system? Or is it a function? Commented Jul 10, 2018 at 16:24
  • Yes getdomain to get the domain, I the code works when i try to get other elements like title echo $html->find('title',0)->plaintext;, but for the class with multiple classes it fails. Commented Jul 11, 2018 at 7:59
  • Firstly, think about what you're naming and why. If getdomain is a method, think also about camelCase and I'm pretty sure that a method or function always requires () if said method/function requires no additional parameters - if it's a defined constant then I would use CAPSCASE, not because you want to shout at other developers, but to alert them that getdomain is a constant. Otherwise it looks like a typo, either a variable with a missing $ at the start, or a function/method with missing () - finally getdomain sounds like a getter so how would one set it? Commented Jul 11, 2018 at 15:26

1 Answer 1

1

I tried this. It is working..

<?php
include (getcwd() . "/simple_html_dom.php");
$html = new simple_html_dom();
$get = file_get_contents("http://localhost/zdemo/php%20selenium/d.json");
$html->load($get);
foreach ($html->find('div[class=_5pbx userContent _3ds9 _3576]') as $link) 
{
    if(isset($link))
    {
          echo $link->plaintext ;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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