Page 2 of 2
Re: Current HTML Page Title
Posted: Tue Dec 09, 2008 5:38 am
by mmj
millsy007 wrote:So I would still need to: "Change page.html to the file you want to get the title from.." each time. I was really hoping there was a way for it to just see the current page and get the value automatically. I know it is easily done in javascript

Tested:
Code: Select all
<?php
$contents = strip_tags(file_get_contents(__FILE__), '<title>');
$title = substr($contents, strpos($contents, '<title>') + 7, strpos($contents, '</title>') - 8);
?>
I use strip_tags to strip the PHP tags, otherwise it would get <title> from the PHP code.
Edit to admin: for some reason when I used
it parsed a smiley in the code.
Re: Current HTML Page Title
Posted: Tue Dec 09, 2008 7:28 am
by millsy007
Thats great thanks
( Just a note for some reason I had to change it to: strpos($contents, '</title>') - 9); to stop a rogue > appearing!? )
Re: Current HTML Page Title
Posted: Tue Dec 09, 2008 8:29 am
by mmj
millsy007 wrote:Thats great thanks
( Just a note for some reason I had to change it to: strpos($contents, '</title>') - 9); to stop a rogue > appearing!? )
NP.
Hmm, thats strange, 8 should be the right number.
Are you sure you don't have this?
<</title>
Re: Current HTML Page Title
Posted: Fri Dec 12, 2008 9:04 am
by millsy007
Now works with 8!?
Also I tried the code to run off using the <H1> Tags but couldnt get it to work. I thought it would be a case of just changing the code to:
Code: Select all
$contents = strip_tags(file_get_contents(__FILE__), '<h1>');
$pagetitle = substr($contents, strpos($contents, '<h1>') + 7, strpos($contents, '</h1>') - 9);
But perhaps not, do the H1 tags not lend themselves to this code?