Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract part of webpage title

Status
Not open for further replies.

johntafasi

Technical User
Feb 10, 2003
8
0
0
US
Hello Group,

I have these two lines on a web page:


The first part of this page's title is <? php code ?>
The second part of this page's title is <? php code ?>


the page's title is:


PHP Learning Center-How to Extract Web Information with PHP

with the right php code the line on the page should read like this:

The first part of this page is PHP Learning Center
The second part of this page is How to Extract Web Information with PHP


Can some one please give me the right code to do this?
 
I have these two lines on a web page:
The first part of this page's title is <? php code ?>
The second part of this page's title is <? php code ?>
Could you post the [!]actual[/!] code that you have on those two lines.

Maybe I'm missing something, but with the page title of your site as "PHP Learning Center-How to Extract Web Information with PHP" it would appear you are doing some kind of coursework... either that, or you are WAY out of your depth on this (given the questions you are asking here).

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
I do not have a code on the webpage now but this is exactly what I want to do. I believe you can use the <title></title> on the page html code.


John
 
Could you restate clearly what it is you want. Your initial post and your follow-up don't really address this. Could you also confirm that this task is work-releated (and has nothing to do with student coursework).

Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
I am not doing any coursework. And I believe that what you are missing is the hyphen between the first and the second part of the webpage's title. I believe you can cause php to search for the first part of the title between the <title> tag and the hyphen that seperate the two parts. I believe that you can cause php to search for the second part of the title between the hyphen and the closing </title> tag.


Thanks

John
 
Unless the page title is also delivered with PHP, you will never be able to do this.

The reason? PHP runs server-side, and the page title is a client-side tag.

Obviously, something that runs on the server will not be able to access something that is not server-side, so unless you can tell us how the page title is being output, you're going to be out of luck.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Give this example page a try. There are many ways to do this - and whilst my solution works fine, I doubt it's the best way to do it.

Code:
<?
/* build up some imaginary web page as a variable */
$rawPageData = '';
$rawPageData .= "<html>\n";
$rawPageData .= "<head>\n";
$rawPageData .= "<title>";
$rawPageData .= "PHP Learning Center-How to Extract Web Information with PHP";
$rawPageData .= "</title>\n";
$rawPageData .= "</head>\n";
$rawPageData .= "<body>\n";
$rawPageData .= "<p>Hello world</p>\n";
$rawPageData .= "</body>\n";
$rawPageData .= "</html>";

/* extract the contents of the <title> tag */
$titleStartPos = strpos($rawPageData,'<title>') + 7;
$titleEndPos = strpos($rawPageData,'</title>');
$title = substr($rawPageData,$titleStartPos,$titleEndPos-$titleStartPos);

/* seperate the title into two fragments */
$titleArray = explode("-", $title);
if (isset($titleArray[0])) $titlePart1 = $titleArray[0];
if (isset($titleArray[1])) $titlePart2 = $titleArray[1];
?>
<html>
<head>
<title><?=$titlePart1?> *** <?=$titlePart2?></title>
</head>

<body>
<p>The original string was <?=$title?></p>
<p>First part of the title was <?=$titlePart1?></p>
<p>Second part of the title was <?=$titlePart2?></p>
</body>
</html>
Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top