* $smarty->load_filter('output','meta_tag'); * * * The above function call should appear BEFORE using * $smarty->display or $smart->fetch. * * @author Milan Hawkins * @copyright Include - http://www.include-digital.com * @link http://blog.include-digital.com/2009/04/03/smarty-seo-plugin-meta_tag * @version 1.0.1 * @param string * @param Smarty */ function smarty_outputfilter_meta_tag($content, &$smarty) { if (is_null($content)) { return; } // fetch current title tag(s) preg_match_all('/\(.*)\<\/title\>/i',$content,$matches,PREG_SET_ORDER); // use first instance of title tag for original title, otherwise default to empty string isset($matches[0][1]) ? $title = $matches[0][1] : $title = ""; // delete extraneous title tags if (count($matches) > 1) { array_shift($matches); foreach ($matches as $match) { $content = str_replace($match[0],'',$content); } } $valid_meta = array('keywords','description','author'); foreach ($valid_meta as $v) { // fetch current meta preg_match_all('/\/i',$content,$matches,PREG_SET_ORDER); // use first instance of meta, deleting duplicates isset($matches[0][1]) ? $$v = $matches[0][1] : $$v = ""; // delete extraneous meta tags if (count($matches) > 1) { array_shift($matches); foreach ($matches as $match) { $content = str_replace($match[0],'',$content); } } } // find meta_tag content added by smarty preg_match_all("/\(.+)\<\/meta_tag\>\r\n/i",$content,$matches,PREG_SET_ORDER); // create arrays for content $title = array($title); $keywords = array($keywords); $description = array($description); $author = array($author); // add meta data to arrays foreach ($matches as $match) { $type = strtolower($match[1]); $method = strtolower($match[2]); $val = str_replace('"','\'',$match[3]); switch ($method) { case 'add': array_push($$type,$val); break; case 'replace': $$type = array($val); break; default: $smarty->trigger_error("meta_tag: unknown method '$method', must be either 'add' or 'replace'"); } } // add meta data to page output foreach ($valid_meta as $v) { $meta = $$v; $meta = implode($meta); $meta = ""; $content = preg_replace('/\/i',$meta,$content); } // create new title tag $title = "".implode($title," - ").""; // insert new title tag into page $content = preg_replace('/\(.*)\<\/title\>/i',$title,$content); // remove redundant meta_tag data added by smarty $content = preg_replace("/\.*\<\/meta_tag\>/i",'',$content); // return output return $content; } ?>