anevins Posted June 9, 2011 Share Posted June 9, 2011 I'm trying to access an attribute in an HTML body tag. I keep getting NULL when I var_dump the variable I'm trying to use to store the attribute. Extract from entire dom NOTE: code is taken from index.php <?php include_once('php/header.php'); include_once('php/simple_html_dom.php'); ?> <body id="home"> <?php $html = file_get_html('index.php'); $body = $html->find('body'); $id = $body->attribute; var_dump($id); ?> Quote Link to comment https://forums.phpfreaks.com/topic/238866-php-simple-html-dom-parser/ Share on other sites More sharing options...
TeNDoLLA Posted June 9, 2011 Share Posted June 9, 2011 Using DOMDocument http://www.php.net/manual/en/class.domdocument.php : $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> </head> <body id="home"> </body> </html>'; $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTML($html); $body = $dom->getElementsByTagName('body'); echo $body->item(0)->getAttribute('id'); // home Quote Link to comment https://forums.phpfreaks.com/topic/238866-php-simple-html-dom-parser/#findComment-1227371 Share on other sites More sharing options...
anevins Posted June 9, 2011 Author Share Posted June 9, 2011 can i give html a file instead of a string? e.g $html = 'index.php'; Quote Link to comment https://forums.phpfreaks.com/topic/238866-php-simple-html-dom-parser/#findComment-1227374 Share on other sites More sharing options...
TeNDoLLA Posted June 9, 2011 Share Posted June 9, 2011 Yes, using loadHTMLFile instead of loadHTML method: http://www.php.net/manual/en/domdocument.loadhtmlfile.php Quote Link to comment https://forums.phpfreaks.com/topic/238866-php-simple-html-dom-parser/#findComment-1227375 Share on other sites More sharing options...
anevins Posted June 9, 2011 Author Share Posted June 9, 2011 Thanks, from $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTMLFile('index.php'); $body = $dom->getElementsByTagName('body'); echo $body->item(0)->getAttribute('id'); // home i get home Quote Link to comment https://forums.phpfreaks.com/topic/238866-php-simple-html-dom-parser/#findComment-1227376 Share on other sites More sharing options...
anevins Posted June 9, 2011 Author Share Posted June 9, 2011 What if I wanted to load the current page from the user, instead of typing a specific page or string. I've tried using $dom->loadHTMLFile('$_SERVER["PHP_SELF"]'); but; fatal error. Do you know how I could do this? Quote Link to comment https://forums.phpfreaks.com/topic/238866-php-simple-html-dom-parser/#findComment-1227384 Share on other sites More sharing options...
TeNDoLLA Posted June 9, 2011 Share Posted June 9, 2011 Im not so sure if I am following you now.. but i think you mean $dom->loadHTMLFile($_SERVER['SCRIPT_NAME']); Its better use SCRIPT_NAME instead of PHP_SELF, since it has some security issues.. (can't remember now what) but you can find em from google if you want to look mroe into it. Also in your code you are passing a string as parameter and not variable. Quote Link to comment https://forums.phpfreaks.com/topic/238866-php-simple-html-dom-parser/#findComment-1227388 Share on other sites More sharing options...
anevins Posted June 9, 2011 Author Share Posted June 9, 2011 Thanks TeNDoLLA, Since my issue that I opened this thread with, has been solved, I will start a new thread with this new issue. Quote Link to comment https://forums.phpfreaks.com/topic/238866-php-simple-html-dom-parser/#findComment-1227406 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.