Current HTML Page Title
Moderator: General Moderators
Current HTML Page Title
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?
I have had a look around but there doesnt seem to be any class/code out there to do this?
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: Current HTML Page Title
Can't you set $pagetitle yourself and stick it inside the <title></title> tags? Like:
Then you've got it to hand in a variable if you want other bits of code to be able to refer to it.
Code: Select all
<?php
$pagetitle = 'Jelly babies were originally launched in 1919';
?>
<title> <?= $pagetitle ?> </title>Re: Current HTML Page Title
if you want to title your pages through php you can use this
it will simply title your pages.
Code: Select all
<html>
<head>
<title>Title Of Your Page<?php echo "$page";?></title>
</head>
Re: Current HTML Page Title
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
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.
@Tassadduq:
Re: Current HTML Page Title
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Current HTML Page Title
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
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
$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
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
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
Change $html to the correct variable which contains the html file contents.
Re: Current HTML Page Title
Sorry mmmj, probably me being dumn but I dont understand what you mean
Re: Current HTML Page Title
Code: Select all
$title = substr(file_get_contents('page.html'), strpos('<title>') + 7, strpos('</title>'));- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Current HTML Page Title
~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
Indeed it does!Chris Corbyn wrote:~mmj, you're still missing the first parameter to strpos()It needs to know the haystack as well as the needle
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
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 