How to Get Remote Webpage's Title in PHP?

by 8 replies
12
Hello, I want to fetch Remote Webpage's Title (<title>...</title>) in PHP. I tried to do so using "fopen" but it didn't work. If someone could post a small snippet of code that fetches the title of remote webpage and prints then it will be really appreciated.
#programming #php #remote #title #webpage
  • There's probably a more efficient way to do it, but this should at least get the job done...

    <?php
    $webpage = file_get_contents('http://www.webpage.com/');
    $title = explode("title>", $webpage);
    $title = str_replace("</", "", $title[1]);
    echo $title;
    ?>

    Note: The above code assumes that the "title" tag in the fetched webpage is always going to be lowercase. If it's not, then change the letter casing right after the 'explode' part. For example...

    $title = explode("Title>", $webpage);
    • [1] reply
    • Thank you very much for the code. The above code fetches the title, but it prints it in small characters, not in capital letters. I want something like this:
      Current: my site title
      I want: My Site Title (or in same letters as it is on source page)

      siteurl=mysite&title=$title

      You can see in above code that the URL would include the site title. If it would have been in a HTML page, I would have used CSS styles to capitalize, but in my case, how can I do so?
      • [1] reply
  • Set the URL of the Web Page
    $url = “http://www.drquincy.com/”;
    Now that’s done we need to get the contents of the title tag. We do this by open up a file stream using file() and reading the contents of the page in and storing it in a variable.
    Load in the File
    $fp = fopen( $url, ‘r’ );

    $content = “”;


    while( !feof( $fp ) ) {

    $buffer = trim( fgets( $fp, 4096 ) );
    $content .= $buffer;

    }
    Then using some we can get the contents of the title tag.
    Get Contents of the Title Tag
    $start = ‘<title>’;
    $end = ‘<\/title>’;

    preg_match( “/$start(.*)$end/s”, $content, $match );
    $title = $match[ 1 ];
    The keywords and description of a web page are stored in the page’s meta tags. Fortunately, PHP has built-in functionality to get meta tag contents so it will be easier to do than the title tag. We call the get_meta_tags() function, which copies all meta tags into an associative array. We then copy the elements of the array we need to some variables.
    Get Meta Tags
    $metatagarray = get_meta_tags( $url );
    $keywords = $metatagarray[ "keywords" ];
    $description = $metatagarray[ "description" ];
    Then all that remains is to output the details (use the variables how you like in your web project):
    Output Details
    echo “<div><strong>URL:</strong> $url</div>\n”;
    echo “<div><strong>Title:</strong> $title</div>\n”;
    echo “<div><strong>Description:</strong> $description</div>\n”;
    echo “<div><strong>Keywords:</strong> $keywords</div>\n”;
    Here’s the complete code listing.
    Complete Code
    <?php

    $url = “http://www.drquincy.com/”;

    $fp = fopen( $url, ‘r’ );

    $content = “”;


    while( !feof( $fp ) ) {

    $buffer = trim( fgets( $fp, 4096 ) );
    $content .= $buffer;

    }

    $start = ‘<title>’;
    $end = ‘<\/title>’;

    preg_match( “/$start(.*)$end/s”, $content, $match );
    $title = $match[ 1 ];

    $metatagarray = get_meta_tags( $url );
    $keywords = $metatagarray[ "keywords" ];
    $description = $metatagarray[ "description" ];

    echo “<div><strong>URL:</strong> $url</div>\n”;
    echo “<div><strong>Title:</strong> $title</div>\n”;
    echo “<div><strong>Description:</strong> $description</div>\n”;
    echo “<div><strong>Keywords:</strong> $keywords</div>\n”;

    ?>

Next Topics on Trending Feed

  • 12

    Hello, I want to fetch Remote Webpage's Title (<title>...</title>) in PHP. I tried to do so using "fopen" but it didn't work. If someone could post a small snippet of code that fetches the title of remote webpage and prints then it will be really appreciated.