Hi
I need to get title of External page
for example : User insert www.google.com
my code return Title of google.com
(my mean of title is <title>Hello</title> //Hello)
any body help me???
Hi
I need to get title of External page
for example : User insert www.google.com
my code return Title of google.com
(my mean of title is <title>Hello</title> //Hello)
any body help me???
There was a thread about something similar to this before, I think it was in the PHP section. I'll try to look for that thread and post a link to it here, but please do a simple search first.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design
<script language="JavaScript">
function whatIsTheTitle()
{
document.write(document.title);
}
</script>
<title>JavaScript Lessons</title>
</head>
<body>
<input type="button" value="Show Title" onClick="whatIsTheTitle()">
</body>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Untested. Copied from working/tested code, though.PHP Code:<?php
ob_start();
include('http://google.com');
$page = ob_get_contents();
ob_end_clean();
list($null,$title) = explode('<title>',$page,2);
list($title,$null) = explode('</title>',$title,2);
echo $title;
?>
EDIT: and here it is with comments:
PHP Code:<?php
ob_start(); //start output buffer
include('http://google.com'); //get page data, and 'output' [to buffer]
$page = ob_get_contents(); //$page = output buffer's contents
ob_end_clean(); //end output buffer
list($null,$title) = explode('<title>',$page,2); //split at <title>, $title = 2nd half
list($title,$null) = explode('</title>',$title,2); //split at </title>, $title = 1st half
echo $title; //output title as string
?>
Last edited by djr33; 01-16-2007 at 11:25 PM.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
I think you should test it.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Well, one variable was wrong.... I had $list from script I borrowed it from. But it works fine. The reason I didn't test was that I can't say it'll work in all cases, depending on how the title is found in text, as I'm not sure exactly how it could vary. Testing on the page(s) required will show that.
The above code is fixed with the right variables.
So.... seems to work.
I changed the code a bit for the test page, to do some extra error checking:comments:PHP Code:<?php
$url= $_GET['url'];
if ($url == '') {$url = 'http://google.com'; }
ob_start();
if (@include($url)) {
$page = ob_get_contents();
}
else {$page = '<title>Page not found.</title>'; }
ob_end_clean();
list($null,$title) = explode('<title>',$page,2);
list($title,$null) = explode('</title>',$title,2);
if ($title == '') $title = $url;
echo $title;
?>When using this, no need to include the get stuff, but some of the error checking might help, especially the line that uses the url if the title doesn't exist, as some pages don't have titles on them.PHP Code:<?php
$url= $_GET['url']; //...page.php?url=URLHERE
if ($url == '') {$url = 'http://google.com'; } //if no url was given, use google
ob_start(); //start output buffer
if (@include($url)) { //if the include returns true, ie if page exists
$page = ob_get_contents(); //get html of page
} //endif
else {$page = '<title>Page not found.</title>'; } //if page doesn't exist, use this fake title
ob_end_clean(); //end output buffer
list($null,$title) = explode('<title>',$page,2); //chop off start before <title>
list($title,$null) = explode('</title>',$title,2); //chop off end after </title>
if ($title == '') $title = $url; //if title is blank (no title on page), use url
echo $title; //output title
?>
Test here:
http://ci-pro.com/misc/phptest/titletest.php
For a custom URL:
http://ci-pro.com/misc/phptest/titletest.php?url=http://your.custom/site/etc
(Note that due to parsing of the URI, it might not work if the URI has, for example, spaces, etc.)
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
It now works on a server I master a couple of sites on but only for sites on that server????
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Try it on my server... outside sites work.
include() is one of the few functions that works with outside files. However, I believe that if there's some security setting enabled, then it won't. I can't remember the details.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
I meant already to have said that, of course it works on your server. It's just a pity that one cannot say with confidence that it will be suitable for any given situation without investigating. Makes it hard sometimes to know if it is the code itself or the server. This also makes it an open question as to whether or not it will do the trick for navid. I realize that PHP is like this and that there are also other reason why any particular bit of PHP code might not work on any particular PHP enabled server, this is a new twist on that for me though.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Bookmarks