I’m using PHP simple dom parser http://simplehtmldom.sourceforge.net/manual.htm.
I’m able to successfully use it and remove an html tag with specific id by doing
$html = str_get_html('<div><div class="two">two</div></div>');
function test($str, $class){
$e = $str->find($class,0);
$e->outertext = '';
echo $str->outertext;
}
test($html, '#two');
My problem is when i try to use the function inside another php class. It doesn’t work. Here’s what i did
$html = new simple_html_dom(); //initialized the object
class Someclass {
public function test($wrap, $class){
global $html;
$html->load($wrap);
$e = $html->find($class,0);
$e->outertext = '';
echo $html->outertext;
}
}
I’m getting an error Fatal error: Call to a member function load() on a non-object
What am i doing wrong. I’m i calling this correctly.
You can create new instance of html dom parser inside your function:
Also make sure that you have included the html parser class in above class using
includeorrequire.BTW, if dom parser is going to be used in various functions of your class, you should create class level variable and store new instance of it when constructing your class like this:
Now you will be able to use
$this->html_parserin different functions of your class.