How to Get Remote Webpage's Title in PHP?

8 replies
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.
#php #remote #title #webpage
  • Profile picture of the author Brandon Tanner
    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);
    Signature

    {{ DiscussionBoard.errors[5100997].message }}
    • Profile picture of the author maheshhari
      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?
      {{ DiscussionBoard.errors[5101260].message }}
      • Profile picture of the author Brandon Tanner
        Originally Posted by maheshhari View Post

        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=

        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?
        The code I provided should return the text exactly as it appears in the fetched webpage's HTML code. If you want to make sure that the first letter in every word is capitalized though, just add this line right before the 'echo' line...

        $title = ucwords($title);
        Signature

        {{ DiscussionBoard.errors[5101567].message }}
        • Profile picture of the author mojojuju
          <?php

          echo get_remotetitle('http://www.yahoo.com/');

          function get_remotetitle($urlpage)
          {
          $dom = new DOMDocument();

          if($dom->loadHTMLFile($urlpage)) {

          $list = $dom->getElementsByTagName("title");
          if ($list->length > 0) {
          return $list->item(0)->textContent;
          }
          }
          }

          ?>

          source
          Signature

          :)

          {{ DiscussionBoard.errors[5101793].message }}
          • Profile picture of the author jared h
            Originally Posted by mojojuju View Post

            <?php

            echo get_remotetitle('link here');

            function get_remotetitle()
            {
            = new DOMDocument();

            if(()) {

            = ("title");
            if ( > 0) {
            return (0)->textContent;
            }
            }
            }

            ?>
            I was about to suggest the same thing. DOMDocument is the way to go here.
            {{ DiscussionBoard.errors[5103013].message }}
        • Profile picture of the author maheshhari
          Originally Posted by Brandon Tanner View Post

          The code I provided should return the text exactly as it appears in the fetched webpage's HTML code. If you want to make sure that the first letter in every word is capitalized though, just add this line right before the 'echo' line...

          = ucwords();
          The code that you've provided returns the webpage title in small caps, not in the letters as the original site has. By the way, thanks for the code
          {{ DiscussionBoard.errors[5112448].message }}
          • Profile picture of the author Brandon Tanner
            Originally Posted by maheshhari View Post

            The code that you've provided returns the webpage title in small caps, not in the letters as the original site has. By the way, thanks for the code
            That code makes the first letter in every word uppercase. Read this...

            PHP: ucwords - Manual
            Signature

            {{ DiscussionBoard.errors[5113752].message }}
  • Profile picture of the author rainso0
    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”;

    ?>
    Signature
    {{ DiscussionBoard.errors[5112038].message }}

Trending Topics