Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author Philip Sturgeon
- * @created 09/02/2009
- */
- class OutputBuffer
- {
- private static $replace_from = array();
- private static $replace_to = array();
- public static $title_separator = ' | ';
- /**
- * Override the title at any point
- *
- * @param string $new_title The new title to output
- */
- public static function set_title($title_parts = array())
- {
- if(!is_array($title_parts))
- {
- $title_parts =& func_get_args();
- }
- $title = htmlspecialchars(trim(implode(self::$title_separator, $title_parts)));
- self::set_regex('#<title>(.+)</title>#', '<title>'.$title.'</title>');
- }
- /**
- * Override head tag (or add it if missing)
- *
- * @param string $new_title The new title to output
- */
- public static function set_head($content = '')
- {
- self::set_regex('#</head>#', "\t".$content."\n</head>");
- }
- /**
- * Override meta data (or add it if missing)
- *
- * @param string $new_title The new title to output
- */
- public static function set_metadata($name = '')
- {
- // Accepts unlimited arguments (2nd+)
- $args =& func_get_args();
- unset($args[0]);
- // Remove HTML and join the arguments
- $content = strip_tags( implode(' ', $args) );
- // Keyword specific replacing
- if($name == 'keywords')
- {
- $content = str_replace(array('_', '-', '(', ')', '!', '?', ':', '"', ',', '.'), '', $content);
- $content = str_replace(array(' ', ',of,', ',and,', ',in,'), ',', $content);
- }
- // If this meta data exists, replace it
- if(strpos(ob_get_contents(), '<meta name="'.$name.'"'))
- {
- self::set_regex('#<meta name="'.$name.'" content="([^"]+)?"#', '<meta name="'.$name.'" content="'.trim($content).'"');
- }
- // Otherwise, add it after the </title> tag
- else
- {
- self::set_regex('#</title>#', '</title>'."\n\t".'<meta name="'.$name.'" content="'.trim($content).'"/>');
- }
- }
- /**
- * Override meta data (or add it if missing)
- *
- * @param string $new_title The new title to output
- */
- public static function set_canonical($link = '')
- {
- global $smarty;
- $url = smarty_outputfilter_linkparser($link, $smarty);
- // If canonical tag exists, replace it
- if(strpos(ob_get_contents(), '<link rel="canonical"'))
- {
- self::set_regex('#<link rel="canonical" href="([^"]+)?"#', '<link rel="canonical" href="'.trim($url).'"');
- }
- // Otherwise, add it after the </title> tag
- else
- {
- self::set_regex('#</title>#', '</title>'."\n\t".'<link rel="canonical" href="'.trim($url).'"/>');
- }
- }
- /**
- * Override the title at any point
- *
- * @param string $new_title The new title to output
- */
- public static function set_regex($from, $to)
- {
- self::$replace_from[] = $from;
- self::$replace_to[] = str_replace('$', '\$', $to);
- }
- /**
- * Override the title at any point
- *
- * @param string $new_title The new title to output
- */
- public static function replace()
- {
- // Get, clean and end the current buffer
- $current_buffer = ob_get_clean();
- // Start a new one
- ob_start();
- // Output the old buffer with the changes as the new buffer
- echo preg_replace(
- self::$replace_from,
- self::$replace_to,
- $current_buffer);
- // Reset the replace pieces in case this is randomly called again
- self::$replace_from = array();
- self::$replace_to = array();
- }
- }
- ?>
Add Comment
Please, Sign In to add comment