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

millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Current HTML Page Title

Post by millsy007 »

I would like to be able to create a $pagetitle variable in php for my pages that will contain the current HTML page title for that page.

I have had a look around but there doesnt seem to be any class/code out there to do this?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Current HTML Page Title

Post by greyhoundcode »

Can't you set $pagetitle yourself and stick it inside the <title></title> tags? Like:

Code: Select all

<?php
$pagetitle = 'Jelly babies were originally launched in 1919';
?>
 
<title> <?= $pagetitle ?> </title>
Then you've got it to hand in a variable if you want other bits of code to be able to refer to it.
User avatar
Tassadduq
Forum Commoner
Posts: 60
Joined: Wed Dec 03, 2008 2:53 pm

Re: Current HTML Page Title

Post by Tassadduq »

if you want to title your pages through php you can use this

Code: Select all

 
<html>
<head>
    <title>Title Of Your Page<?php echo "$page";?></title>
</head>
 
it will simply title your pages.
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Current HTML Page Title

Post by millsy007 »

I want to sort of do the opposite, get the title of the current HTML page that has already been named buy someone else (using adobe contribute) and then use this value in php code?
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Current HTML Page Title

Post by mmj »

You can either use some string functions or something to query the dom.

@Tassadduq: :dubious: Putting the variable in a string is a nice way to degrade performance for no reason.
matt1234
Forum Commoner
Posts: 44
Joined: Wed Nov 26, 2008 9:43 pm

Re: Current HTML Page Title

Post by matt1234 »

Hmmm.. I'm not quite sure that you'd be able to do that, unfortunately. I don't know of a variable that PHP would have for a title that is already put in by HTML. You may just have to go in to the pages and add a line in PHP in the <title> where it picks up whatever is typed out for the title as a string.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Current HTML Page Title

Post by Chris Corbyn »

The cleanest approach is to parse the DOM and grab the title with XPath. But in order for this to work the (X)HTML needs to be near 100% valid (a good thing).
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Current HTML Page Title

Post by mmj »

This should work, didn't test it though.

$title = substr($html, strpos('<title>') + 7, strpos('</title>'));

However what Chris noted is correct imo.

Note: not sure how this would work with utf-8 documents and too lazy to find out :P
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Current HTML Page Title

Post by millsy007 »

Thanks but I get the error:

Warning: Wrong parameter count for strpos() in /home/beachh/public_html/xmltest2.php on line 3

Unfortunately I have no idea what that means :(
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Current HTML Page Title

Post by mmj »

Change $html to the correct variable which contains the html file contents.
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Current HTML Page Title

Post by millsy007 »

Sorry mmmj, probably me being dumn but I dont understand what you mean
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Current HTML Page Title

Post by mmj »

Code: Select all

$title = substr(file_get_contents('page.html'), strpos('<title>') + 7, strpos('</title>'));
Change page.html to the file you want to get the title from..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Current HTML Page Title

Post by Chris Corbyn »

~mmj, you're still missing the first parameter to strpos() ;) It needs to know the haystack as well as the needle ;)
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Current HTML Page Title

Post by mmj »

Chris Corbyn wrote:~mmj, you're still missing the first parameter to strpos() ;) It needs to know the haystack as well as the needle ;)
Indeed it does! :oops:

I think I was writing JS at the time, you get uses to the OOP behavior of not having to pass the object. :P
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Current HTML Page Title

Post by millsy007 »

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 :(
Post Reply