Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: Get Title

  1. #1
    Join Date
    Dec 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get Title

    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???

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    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

  3. #3
    Join Date
    Jan 2007
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    <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>

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by trippin View Post
    <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>
    That would work great if you wanted to obliterate the page. Oh, wait - you'd have to get Google's permission to put that code on their page. I somehow don't think Google would go for that.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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;
    ?>
    Untested. Copied from working/tested code, though.


    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

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think you should test it.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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:
    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;
    ?>
    comments:
    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
    ?>
    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.

    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

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •