FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Echoing Titles





phileplanet
Does anyone know how to echo a title from an external page.
Like echo the title of page6.php.
kv
I don't think there is any straight forward way. you may need to read the file, grep for the <title> and </title>, use the word in between. You may also try using regular expression.
phileplanet
kv wrote:
I don't think there is any straight forward way. you may need to read the file, grep for the <title> and </title>, use the word in between. You may also try using regular expression.

Wow sorry but I don't understand. Could you rephrase it or possibly give me an example?
leodv
I suppose what he meant was to use a server side scripting language to request a external page, then parse the title from that received page. It would take some effort to parse the title out tho.
phileplanet
leodv wrote:
I suppose what he meant was to use a server side scripting language to request a external page, then parse the title from that received page. It would take some effort to parse the title out tho.


Wow, ok well i'll try to get that worked out. Thanks.
BlackSkad
Code:
<?php
$file = file ('page6.php');
$title = "";

foreach ($file as $line)
{
   if ($title != "")
       break;
      
   if (preg_match('#<title>([^.]+)</title>#', $line, $returnValue))
   {
      $title = $returnValue[0];
   }
}

$title = ($title == "") ? "not found" : $title;

echo "The title is \n";
echo $title;
?>


This should do the job.
It opens a file and places it into an array.
With the foreach-loop we check each line.
The first if-statement states that the loop must continue untill the title isn't empty or all lines have been searched.
The second if searches the line for <title>something</title> and places each match of something in the array $retrunValue. To get the first value, you use $returnValue[0], second is $returnValue[1], ...
Thendo so more control the change an empty title in 'not found' and then display it.

I hope this helpes you Smile
phileplanet
BlackSkad wrote:

This should do the job.
It opens a file and places it into an array.
With the foreach-loop we check each line.
The first if-statement states that the loop must continue untill the title isn't empty or all lines have been searched.
The second if searches the line for <title>something</title> and places each match of something in the array $retrunValue. To get the first value, you use $returnValue[0], second is $returnValue[1], ...
Thendo so more control the change an empty title in 'not found' and then display it.

I hope this helpes you Smile

It keeps saying that the title cannot be found.

I thinks this is because i don't have a <title>Title</title> line in my page. I actually have a $title= and then I pull in another file that will echo the $title in the <title>. Could that be the problem?
phileplanet
Just to add on to what i just said, is there any way you an echo a variable from an external file?
mathiaus
I've edited the original a script a little

Code:
<?php

$file = file ('untitled1.php');
$title = "";

foreach ($file as $line)
{
   if ($title != "")
       break;
     
   if (preg_match('#title=\"([^.]+)\";#', $line, $returnValue))
   {
      $title = $returnValue[0];
   }
}

$title = ($title == "") ? "not found" : $title;

$search = array ('/title/',
                 '@\=@',         
                 '@\"@',
                 '@\;@');                   
$replace = array ('',
                 '',
                 '',
                 '');

echo "The title is \n";
echo preg_replace($search, $replace, $title);
?>


Hopefully you can see from that what I've done.
Although it could be simpler (a lot simpler!)
BlackSkad
phileplanet wrote:

It keeps saying that the title cannot be found.

I thinks this is because i don't have a <title>Title</title> line in my page. I actually have a $title= and then I pull in another file that will echo the $title in the <title>. Could that be the problem?


That is indeed the problem Smile. I don't know if that file does something specific (output ot other resource-requesting tasks). If not, you could just include the file and use that variable.
Maybe you might try to put all title in a separate file and choose the right title with a switch-statement or someting. Everything depents on the context of your script (how you assign your variables, etc... ).
phileplanet
BlackSkad wrote:

That is indeed the problem Smile. I don't know if that file does something specific (output ot other resource-requesting tasks). If not, you could just include the file and use that variable.
Maybe you might try to put all title in a separate file and choose the right title with a switch-statement or someting. Everything depents on the context of your script (how you assign your variables, etc... ).


Works like a charm. Thanks! I think that's 2 for 2 BlackSkad Very Happy
BlackSkad
You're most welcome, we're here to help eachother Smile
Related topics
your specs
Does anyone know how to get your site to appear higher ?
How many hours do you spend on games each day?
Arles Image Web Page Creator, creating the best images site
best booke in your mind
new Nintendo Revolution
3 Jokes
Basic Search Engine Optimizing
Harry Potter 6 - The Half-Blood Prince *(ENDING REVEALED)*
Unique joomla page titles?
The Titles of Forums
Anticipated Titles of 07 [Xbox 360] (trailers included)
PS3 Upcoming titles
Can someone translate this hungarian song titles?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.