How can I use "PHP Simple HTML DOM Parser" to get the contents of an <h1></h1> tag?

Posted by KeriLynn on Stack Overflow See other posts from Stack Overflow or by KeriLynn
Published on 2010-05-14T14:01:09Z Indexed on 2010/05/14 14:04 UTC
Read the original article Hit count: 149

Filed under:
|

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/, 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.google.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!! =)

© Stack Overflow or respective owner

Related posts about php

Related posts about html-parsing