PHP to get html & metadata for website


Following script can be used to get the first part of a remote file to parse the html elements in local script. eregi() function is used here to parse the meta keywords, the meta description and title element.
Extra rules and regex patterns can be added for more meta elements. We can use this script for adding new links in to a linklist.
Php script is as below:


  <?php   $page_title = "NA";   $meta_descr = "NA";   $meta_keywd = "NA";   if ($handle = @fopen("http://www.your_url.com", "r")) {   $content = "";   while (!feof($handle)) {   $part = fread($handle, 1024);   $content .= $part;   if (eregi("</head>", $part)) break;   }   fclose($handle);   $lines = preg_split("/\r?\n|\r/", $content); // turn the content in rows   $is_title = false;   $is_descr = false;   $is_keywd = false;   $close_tag = ($xhtml) ? " />" : ">"; // new in ver. 1.01   foreach ($lines as $val) {   if (eregi("<title>(.*)</title>", $val, $title)) {   $page_title = $title[1];   $is_title = true;   }   if (eregi("<meta name=\"keywords\" content=\"(.*)\"([[:space:]]?/)?>", $val, $keywd)) {   $meta_keywd = $keywd[1];   $is_keywd = true;   }   if (eregi("<meta name=\"description\" content=\"(.*)\"([[:space:]]?/)?>", $val, $descr)) {   $meta_descr = $descr[1];   $is_descr = true;   }   if ($is_title && $is_descr && $is_keywd) break;   }   }   ?>

No comments:

Post a Comment