Get ready for BLACK FRIDAY shopping starting in

Forums

PHP

This topic is locked

Can you retireve the page title via PHP

Posted 29 Jan 2002 13:20:30
1
has voted
29 Jan 2002 13:20:30 Stephen Bateman posted:
Does anyone know a way of retrieving the page title via a PHP variable ?

I want to use the following script:

javascript:window.external.AddFavorite('<?php echo $HTTP_HOST; echo $PHP_SELF ?>', 'My Page')

Which works fine, but I need to make the "My Page" title dynamic as such.

I'm sure it's possible but I just can't find it on the PHP site.

Stephen

PS. Does anyone know any good PHP discussion boards, I sometime think users here must get annoyed with some of my dum questions...lol

Replies

Replied 29 Jan 2002 22:28:19
29 Jan 2002 22:28:19 Bruno Mairlot replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
Does anyone know a way of retrieving the page title via a PHP variable ?

I want to use the following script:

javascript:window.external.AddFavorite('&lt;?php echo $HTTP_HOST; echo $PHP_SELF ?&gt;', 'My Page')

Which works fine, but I need to make the "My Page" title dynamic as such.

I'm sure it's possible but I just can't find it on the PHP site.
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Unfortunately, Stephen,

there is some way to do that specifically in PHP, but it is quite hard, let me explain why. But before, just a word. I think it would be much much more easier to do that in JavaScript than in PHP.

The title of the page, is contained in the &lt;title&gt;...&lt;/title&gt; tag.
Therefore, this is HTML code and not PHP code. When the PHP engine parse and run your code, your HTML page doesn't exists yet completely.

Let's suppose your PHP code is in the body of the document, then, your title has already been sent to Apache core to be sent to the browser. There is no way to reverse the order of your file, just to find it.

And, as you can't do backward execution, you can't either do forward execution... If your code is before the title tag, you can't go further to know about it.


The only conceivable way to do this, is to get the name of the file you're currently executing. $PHP_SELF is a good start.
Then, in your execution, open this file with <b>fopen($PHP_SELF,"r"</b>, then read the file as long as you don't match &lt;title&gt;.

for example :

<pre id=code><font face=courier size=2 id=code>
$fh = fopen($DOCUMENT_ROOT.$PHP_SELF,"r";
$found=false;
$title="";
while(!feof($fh) && !found){
if(preg_match("/&lt;title&gt;(.*)&lt\\\/title&gt;/",fget($fh,4096),$matches)){
found=true;
$title=$matches[1];
}
}
fclose($fh);
if($found){
// Here title is correct
.
.
}
else{
// this page doesn't contain &lt;title&gt;
}
</font id=code></pre id=code>

I just wrote this right now, so it might work, but it might not <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>
There is a small point, if the title section is bigger than 4096 bytes, you must find a way to get rid of this. Though, 4096 bytes should be a good value.

Hope it helps.

Bruno

--- Better to die trying, than never try at all ---
Replied 29 Sep 2007 07:31:45
29 Sep 2007 07:31:45 Marcos Silva replied:
Another way to get title:

function get_remotetitle($urlpage)
{
$dom = new DOMDocument();
if($dom-&gt;loadHTMLFile($urlpage)) {

$list = $dom-&gt;getElementsByTagName("title";
if ($list-&gt;length &gt; 0) {
$this-&gt;m_title = $list-&gt;item(0)-&gt;textContent;
}
}

Regards!

Reply to this topic