Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.