views:

660

answers:

3

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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

+2  A: 

You're not saying how your HTML is structured, but if you want to find the h1 with the ID content you need to use

foreach($html->find('h1#content') as $element){

the way you are doing it right now, it says "find any h1 element within another element with the ID content".

Pekka
Thanks for your answer, that is what I wanted it to do. I tried just now a few different ways- giving the h1 tag an id of "title" and changing the php code to h1#title, but it still isn't echoing anything :(
KeriLynn
@Keri can you show the HTML you are querying?
Pekka
Okay, it's up, thanks again for your help
KeriLynn
A: 

I would restructure your code a little. Basically, you are trying to get the content in h1 before it is populated. In your test.php I would define an array with meta data and then include the header.
Like so:

test.php

<?php 
$meta = array();
$meta['title'] = "This should be the title";

include '/home/dreami14/public_html/design/includes/head.php' 

?>

<div id="main">
<h1><?php echo $meta['title'] ?></h1>
<p>Blah blah</p>
</div>

</body>
</html>

head.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>
 <?php echo (isset($meta) && isset($meta['title'])) ? $meta['title'] : "Default title"; ?>
   </title>
   <link rel="stylesheet" type="text/css" href="/stylesheet.css" />
</head>
<body>

But if you start to do more complicated stuff, you should have a look at the Model-View-Controller design pattern and e.g. the Zend framework, which implements it.

Felix Kling
A: 

I thinks it's the way, only print child content:

html = file_get_html($url);
foreach($ret->children as $child) {  
   echo $child;
}
superedi