Current HTML Page Title

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Current HTML Page Title

Post 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.
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Current HTML Page Title

Post by millsy007 »

Thats great thanks :D
( Just a note for some reason I had to change it to: strpos($contents, '</title>') - 9); to stop a rogue > appearing!? )
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Current HTML Page Title

Post by mmj »

millsy007 wrote:Thats great thanks :D
( 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>
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Current HTML Page Title

Post 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?
Post Reply