I'm new to PHP =) Right now I am using PHP includes for my site template. I have my header, containing all my <head></head>
info. What I want to do is write a code that will take the contents of the <h1></h1>
tag from the page, and echo it into the <title></title>
tag in my header.php include.
I got the PHP Simple HTML DOM Parser from here: [http://simplehtmldom.sourceforge.net/][1], and I found a code (I forget where in all my googling) that goes like this:
<?php
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$html = file_get_html('http://www.myurl.com/');
foreach($html->find('#content h1') as $element){
echo $element->plaintext;}
?>
That I think is supposed to echo the h1 tag contents? Like I said, I'm new to PHP and I only know the basics, and I don't know really know any OOP (yet), so I'm sorry if I'm asking a dumb question.
It looks like it's getting the current page, then putting the contents of the h1 tag into the variable $element, and then echoing it. But nothing happens when I put it into my page. Can anyone help me with what I'm doing wrong? Thank you for reading!! =)
EDIT: Here's my HTML
From the header.php file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
/* current page url */
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
<?php include '/home/dreami14/public_html/simplehtmldom/simplehtmldom/simple_html_dom.php' ?>
<title>
<?php
$url = curPageURL();
$html = file_get_html($url);
foreach($html->find('#main h1') as $element){
echo $element->plaintext;}
?></title>
<link rel="stylesheet" type="text/css" href="/stylesheet.css" />
</head>
<body>
From test.php:
<?php include '/home/dreami14/public_html/design/includes/head.php' ?>
<div id="main">
<h1>This should be the title</h1>
<p>Blah blah</p>
</div>
</body>
</html>
I don't get any errors, but my <title></title>
is empty.
Edit to add: also, I echoed $url in the document itself so I know that part is working