Jump to content

PHP Simple HTML DOM Parser


anevins

Recommended Posts

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/238866-php-simple-html-dom-parser/
Share on other sites

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.