PHPBuilder - MetaEngine



RSS Twitter
Snippets Other

MetaEngine

by: Brian Douros
|
August 7, 2001

Version: 1.2

Type: Function

Category: Other

License: Other

Description: This function was created because the get_meta_tags() function does not properly return meta information if the requested file is not formated correctly or if newline, return, and tab characters are present within the meta tag. NOTES: This function will only return meta information for title, keywords, and description.



<?php 

  /***********************************************************************************
  FILE:         metaengine.php
  FUNCTION:     MetaEngine();
  AUTHOR:       Brian Douros
  EMAIL:        bjdouros@enteract.com
  CREATED:      08/27/2000
  MODIFIED:     08/07/2001
  INPUT:        Any URL value, ($url);
  OUTPUT:       An array of meta tag information, where the name attribute of the 
                meta tag is the key value of the array. (e.g. $metatag[title], 
                $metatag[keywords], $metatag[description])
  DESCRIPTION:  This function was created because the get_meta_tags() function does 
                not properly return meta information if the requested file is not 
                formated correctly or if newline, return, and tab characters are 
                present within the meta tag.
  NOTES:        This function will only return meta information for title, keywords, 
                and description.
  COPYTRIGHT:   Brian Douros, All Rights Reserved.  This script may be used freely 
                under the GNU General Public License but may not be reproduced for 
                sale in any manner except with written permsision from Brian Douros. 
  ************************************************************************************/

function MetaEngine($url) {
  // Pattern for meta title
  $p_title[0] = '(<title>)([a-zA-Z_0-9@!%-;&`,\'\+\$\?\.\n\t\r ]+)';
  $p_title[1] = '(<meta[[:space:]]+name[ \'\"]*=[ \'\"]*title[ \'\"]*[[:space:]]*content[ \'\"]*=[ \'\"]*)([a-zA-Z_0-9@!%-;&`,\'\+\$\?\.\n\t\r ]+)';

  // Pattern for meta description
  $p_description[0] = '(<meta[[:space:]]+name[ \'\"]*=[ \'\"]*description[ \'\"]*[[:space:]]*content[ \'\"]*=[ \'\"]*)([a-zA-Z_0-9@!%-;&`,\'\+\$\?\.\n\t\r ]+)';
  
  // Pattern for meta keywords
  $p_keywords[0] = '(<meta[[:space:]]+name[ \'\"]*=[ \'\"]*keywords[ \'\"]*[[:space:]]*content[ \'\"]*=[ \'\"]*)([a-zA-Z_0-9@!%-;&`,\'\+\$\?\.\n\t\r ]+)';
  $p_keywords[1] = '(</head>)(.+)';
  
  // Fetch file into an array
  if(!($file = @file( $url, "r" ))) {
    $keywords = 'Not Available';
    $description = 'Not Available';
    $title = 'Not Available';
  }
  else {
	  // Turn array into a string using a space as the delimiter.
    $target = @implode( " ", $file);
    $target = stripslashes($target);
		
    // Remove tab, return, and newline characters.
    $pat = "\n";
    $repl = " ";
    $target = ereg_replace($pat, $repl, $target);
    $pat = "\t";
    $repl = " ";
    $target = ereg_replace($pat, $repl, $target);
    $pat = "\r";
    $repl = " ";
    $target = ereg_replace($pat, $repl, $target);

    // Evaluate string with regular expression and find match for title.
    if(eregi($p_title[0], $target, $match)) { 
      $title = $match[2];
    } //End if 
    elseif(eregi($p_title[1], $target, $match)) {
      $title = $match[2];
    } //End else if
    else {
      $title = 'Not Available';
    } //End else

    // Evaluate string with regular expression and find match for description.
    if(eregi($p_description[0], $target, $match)) { 
      $description = $match[2];
    } //End if
    else {
      $description = 'Not Available';
    } //End else

    // Evaluate string with regular expression and find match for keywords.
    if(eregi($p_keywords[0], $target, $match)) { 
      $keywords = $match[2];
    } //End if
    /* If no meta tag content is presend for keywords use document text as keywords
       starting after the </head> tag. */
    elseif(eregi($p_keywords[1], $target, $match)) {
      //Remove HTML and PHP tags
      $match[2] = strip_tags($match[2]);
      //Strip white spaces before and after string
      $match[2] = trim($match[2]);
      //Limit size of string to 1000 characters starting at the 100th character
      $match[2] = substr($match[2], 100, 1100);
      $keywords = trim($match[2]);
    } //End else
    else {
      $keywords = 'Not Available';
    } //End else
  } //End if

  $metatag[title] = $title;
  $metatag[description] = $description;
  $metatag[keywords] = $keywords;
  
  return $metatag;
} //End MetaEngine method

?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Brian Douros

Comment:



Comment:

(Maximum characters: 1200). You have characters left.