Guest User

Untitled

a guest
May 28th, 2012
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. <?php
  2. function getUrlData($url)
  3. {
  4. $result = false;
  5.  
  6. $contents = getUrlContents($url);
  7.  
  8. if (isset($contents) && is_string($contents))
  9. {
  10. $title = null;
  11. $metaTags = null;
  12.  
  13. preg_match('/<title>([^>]*)<\/title>/si', $contents, $match );
  14.  
  15. if (isset($match) && is_array($match) && count($match) > 0)
  16. {
  17. $title = strip_tags($match[1]);
  18. }
  19.  
  20. preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' .'[lang="]*[^>"]*["]*'.'[\s]*content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);
  21. if (isset($match) && is_array($match) && count($match) == 3)
  22. {
  23. $originals = $match[0];
  24. $names = $match[1];
  25. $values = $match[2];
  26.  
  27. if (count($originals) == count($names) && count($names) == count($values))
  28. {
  29. $metaTags = array();
  30.  
  31. for ($i=0, $limiti=count($names); $i < $limiti; $i++)
  32. {
  33. $metaname=strtolower($names[$i]);
  34. $metaname=str_replace("'",'',$metaname);
  35. $metaname=str_replace("/",'',$metaname);
  36. $metaTags[$metaname] = array (
  37. 'html' => htmlentities($originals[$i]),
  38. 'value' => $values[$i]
  39. );
  40. }
  41. }
  42. }
  43. if(sizeof($metaTags)==0) {
  44. preg_match_all('/<[\s]*meta[\s]*content="?' . '([^>"]*)"?[\s]*' .'[lang="]*[^>"]*["]*'.'[\s]*name="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);
  45.  
  46. if (isset($match) && is_array($match) && count($match) == 3)
  47. {
  48. $originals = $match[0];
  49. $names = $match[2];
  50. $values = $match[1];
  51.  
  52. if (count($originals) == count($names) && count($names) == count($values))
  53. {
  54. $metaTags = array();
  55.  
  56. for ($i=0, $limiti=count($names); $i < $limiti; $i++)
  57. {
  58. $metaname=strtolower($names[$i]);
  59. $metaname=str_replace("'",'',$metaname);
  60. $metaname=str_replace("/",'',$metaname);
  61. $metaTags[$metaname] = array (
  62. 'html' => htmlentities($originals[$i]),
  63. 'value' => $values[$i]
  64. );
  65. }
  66. }
  67. }
  68.  
  69. }
  70.  
  71. $result = array (
  72. 'title' => $title,
  73. 'metaTags' => $metaTags
  74. );
  75. }
  76.  
  77. return $result;
  78. }
  79. function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
  80. {
  81. $result = false;
  82. //global $contents;
  83. $contents = @file_get_contents($url);
  84.  
  85. // Check if we need to go somewhere else
  86.  
  87. if (isset($contents) && is_string($contents))
  88. {
  89. preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $contents, $match);
  90.  
  91. if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1)
  92. {
  93. if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections)
  94. {
  95. return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
  96. }
  97.  
  98. $result = false;
  99. }
  100. else
  101. {
  102. $result = $contents;
  103. }
  104. }
  105.  
  106. return $contents;
  107. }
  108. ?>
  109. //------------------Usage--------------------------------
  110. $Domain='<a class="linkclass" href="http://www.samplephpcodes.com">http://www.samplephpcodes.com</a>'; // website
  111. $result = getUrlData($Domain);
  112. if($result['title']=="") {
  113. $title="No Data Available";
  114. } else {
  115. $title=$result['title'];
  116. }
  117. if($result['metaTags']['description']['value']=="") {
  118. $description="No Data Available";
  119. } else {
  120. $description=$result['metaTags']['description']['value'];
  121. }
  122. if($result['metaTags']['keywords']['value']=="") {
  123. $keywords="No Data Available";
  124. } else {
  125. $keywords=$result['metaTags']['keywords']['value'];
  126. }
  127. echo '<br>Title - '.$title;
  128. echo '<br>Description - '.$description;
  129. echo '<br>Keywords - '.$keywords;
  130. ?>
Advertisement
Add Comment
Please, Sign In to add comment