Advertisement
metalx1000

Get Website's Title and Image Meta data

Sep 6th, 2015
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.28 KB | None | 0 0
  1. <?php
  2. $file = 'http://www.winknews.com/2015/08/06/4-rescued-from-overnight-house-fire-in-naples/';
  3.  
  4. // the following line prevents the browser from parsing this as HTML.
  5. //header('Content-Type: text/plain');
  6.  
  7. // get the file contents, assuming the file to be readable (and exist)
  8. $contents = file_get_contents($file);
  9. $contents = str_replace("<", "\n<", $contents);
  10.  
  11. $title = getTitle($contents);
  12. print "$title<br>";
  13.  
  14. $img = getIMG($contents);
  15. print "$img<br>";
  16.  
  17. function getIMG($contents){
  18.   $searchfor = 'og:image';
  19.   // escape special characters in the query
  20.   $pattern = preg_quote($searchfor, '/');
  21.   // finalise the regular expression, matching the whole line
  22.   $pattern = "/^.*$pattern.*\$/m";
  23.   preg_match_all($pattern, $contents, $matches);
  24.   $img = implode("\n", $matches[0]);
  25.   $img = explode('"', $img);
  26.   return "$img[3]";
  27. }
  28.  
  29. function getTitle($contents){
  30.   $searchfor = '<title>';
  31.   // escape special characters in the query
  32.   $pattern = preg_quote($searchfor, '/');
  33.   // finalise the regular expression, matching the whole line
  34.   $pattern = "/^.*$pattern.*\$/m";
  35.   preg_match_all($pattern, $contents, $matches);
  36.   $title = implode("\n", $matches[0]);
  37.   $title = explode('>', $title);
  38.   $title = explode('<', $title[1]);
  39.   return "$title[0]";
  40. }
  41.  
  42.  
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement