Page 1 of 2
Current HTML Page Title
Posted: Fri Dec 05, 2008 4:55 am
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?
Re: Current HTML Page Title
Posted: Fri Dec 05, 2008 5:07 am
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.
Re: Current HTML Page Title
Posted: Fri Dec 05, 2008 12:34 pm
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.
Re: Current HTML Page Title
Posted: Fri Dec 05, 2008 2:23 pm
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?
Re: Current HTML Page Title
Posted: Fri Dec 05, 2008 3:21 pm
by mmj
You can either use some string functions or something to query the dom.
@Tassadduq:

Putting the variable in a string is a nice way to degrade performance for no reason.
Re: Current HTML Page Title
Posted: Fri Dec 05, 2008 4:58 pm
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.
Re: Current HTML Page Title
Posted: Sat Dec 06, 2008 2:28 am
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).
Re: Current HTML Page Title
Posted: Sat Dec 06, 2008 9:00 am
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

Re: Current HTML Page Title
Posted: Sat Dec 06, 2008 9:20 am
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

Re: Current HTML Page Title
Posted: Sat Dec 06, 2008 9:30 am
by mmj
Change $html to the correct variable which contains the html file contents.
Re: Current HTML Page Title
Posted: Sat Dec 06, 2008 9:50 am
by millsy007
Sorry mmmj, probably me being dumn but I dont understand what you mean
Re: Current HTML Page Title
Posted: Sat Dec 06, 2008 1:27 pm
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..
Re: Current HTML Page Title
Posted: Sat Dec 06, 2008 3:44 pm
by Chris Corbyn
~mmj, you're still missing the first parameter to strpos()

It needs to know the haystack as well as the needle

Re: Current HTML Page Title
Posted: Sun Dec 07, 2008 4:06 am
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!
I think I was writing JS at the time, you get uses to the OOP behavior of not having to pass the object.

Re: Current HTML Page Title
Posted: Mon Dec 08, 2008 5:18 am
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
