Changer automatiquement le title en fonction de l’action

Comme j’oublie tout le temps de définir la balise Title spécifique à chaque page je me suis dis que mon ami symfony pourrai peut être penser à ma place, voici donc mon code

Dans lib/filter/sfAutoMetaFilter.class.php

<?php
/**
 * Manage MetaTags 
 * @author Cédric Lombardot 
 */
 
class sfAutoMetaFilter extends sfFilter{
    public function execute ($filterChain)
      {
        $filterChain->execute();
        /*
         * Recherche le h1 pour mettre le title
         */
 
 
        $html=$this->getContext()->getResponse()->getContent(); 
        $title=$this->get_title($html);
        $h1s=$this->get_h1($html);
        if($h1s[1]==0){
            $h2s=$this->get_h2($html);
            if($h2s[1]!=0){
                $this->getContext()->getResponse()->setContent($this->set_title($html,(($title)?($title.' - ' ):'').strip_tags($h2s[0][0])));
            }
        }else{
            $this->getContext()->getResponse()->setContent($this->set_title($html,(($title)?($title.' - ' ):'').strip_tags($h1s[0][0])));
        }
 
 
 
 
      }
 
      // retrieve all h1 tags
        protected function get_h1($html){
            $h1tags = preg_match_all("/()(.*)(<\/h1>)/isxmU",$html,$patterns);
            $res = array();
            array_push($res,$patterns[2]);
            array_push($res,count($patterns[2]));
            return $res;
        } 
        protected function get_h2($html){
            $h2tags = preg_match_all("/()(.*)(<\/h2>)/isxmU",$html,$patterns);
            $res = array();
            array_push($res,$patterns[2]);
            array_push($res,count($patterns[2]));
            return $res;
        } 
        protected function get_title($html){
            $title = preg_match("/()(.*)(<\/title>)/isxmU",$html,$patterns);
            return isset($patterns[2])?$patterns[2]:'';
        } 
        protected function set_title($html,$title){
            return preg_replace("/(',$html);
        } 
}
?>
</pre>
<p>Et dans le filters.yml</p>
<pre name="code" class="php">autometa:
  class: sfAutoMetaFilter
</pre>
					</div><!-- .entry-content -->
		
		<footer class="entry-meta">
			Cette entrée a été publiée dans <a href="http://blog.lombardot.fr/category/symfony/" rel="category tag">Symfony</a>, et marquée avec <a href="http://blog.lombardot.fr/tag/filter/" rel="tag">filter</a>, <a href="http://blog.lombardot.fr/tag/symfony/" rel="tag">Symfony</a>, le <a href="http://blog.lombardot.fr/2010/05/05/changer-automatiquement-le-title/" title="3 h 27 min" rel="bookmark"><time class="entry-date" datetime="2010-05-05T03:27:53+00:00">5 mai 2010</time></a> <span class="by-author">par <span class="author vcard"><a class="url fn n" href="http://blog.lombardot.fr/author/admin/" title="Afficher tous les articles par Cédric Lombardot" rel="author">Cédric Lombardot</a></span></span>.								</footer><!-- .entry-meta -->
	</article><!-- #post -->

				<nav class="nav-single">
					<h3 class="assistive-text">Navigation des articles</h3>
					<span class="nav-previous"><a href="http://blog.lombardot.fr/2010/04/14/savoir-si-un-widget-est-requis/" rel="prev"><span class="meta-nav">←</span> Savoir si un widget est requis</a></span>
					<span class="nav-next"><a href="http://blog.lombardot.fr/2010/05/21/debuggez-vos-requettes-sql-propel/" rel="next">Débuggez vos requêttes SQL / Propel <span class="meta-nav">→</span></a></span>
				</nav><!-- .nav-single -->

				
<div id="comments" class="comments-area">

	
	
									<div id="respond" class="comment-respond">
				<h3 id="reply-title" class="comment-reply-title">Laisser un commentaire <small><a rel="nofollow" id="cancel-comment-reply-link" href="/2010/05/05/changer-automatiquement-le-title/#respond" style="display:none;">Annuler la réponse.</a></small></h3>
									<p class="must-log-in">Vous devez <a href="http://blog.lombardot.fr/wp-login.php?redirect_to=http%3A%2F%2Fblog.lombardot.fr%2F2010%2F05%2F05%2Fchanger-automatiquement-le-title%2F">être connecté</a> pour rédiger un commentaire.</p>												</div><!-- #respond -->
			
</div><!-- #comments .comments-area -->
			
		</div><!-- #content -->
	</div><!-- #primary -->


			<div id="secondary" class="widget-area" role="complementary">
			<aside id="categories-3" class="widget widget_categories"><h3 class="widget-title">Catégories</h3><select name='cat' id='cat' class='postform' >
	<option value='-1'>Choisir une catégorie</option>
	<option class="level-0" value="11">Divers  (7)</option>
	<option class="level-1" value="12">   Shell  (7)</option>
	<option class="level-0" value="10">JQuery  (1)</option>
	<option class="level-0" value="1">Non classé  (4)</option>
	<option class="level-0" value="3">Symfony  (10)</option>
	<option class="level-1" value="8">   Doctrine  (1)</option>
	<option class="level-1" value="6">   Form  (1)</option>
	<option class="level-1" value="4">   Propel  (3)</option>
	<option class="level-1" value="9">   widget  (1)</option>
</select>

<script type='text/javascript'>
/* <![CDATA[ */
	var dropdown = document.getElementById("cat");
	function onCatChange() {
		if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
			location.href = "http://blog.lombardot.fr/?cat="+dropdown.options[dropdown.selectedIndex].value;
		}
	}
	dropdown.onchange = onCatChange;
/* ]]> */
</script>

</aside><aside id="rss-3" class="widget widget_rss"><h3 class="widget-title"><a class='rsswidget' href='http://www.symfony-project.org/plugins/recently.rss'><img style='border:0' width='14' height='14' src='http://blog.lombardot.fr/wp-includes/images/rss.png' alt='RSS' /></a> <a class='rsswidget' href='http://www.symfony-project.org/plugins/'>Les plugins symfony</a></h3><ul><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfDoctrineCronManagerPlugin/1_0_0'>sfDoctrineCronManagerPlugin 1.0.0</a></li><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfCacheTaggingPlugin/4_4_0'>sfCacheTaggingPlugin 4.4.0</a></li><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfCacheTaggingPlugin/4_3_0'>sfCacheTaggingPlugin 4.3.0</a></li><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfDoctrineApplyPlugin/1_2_2'>sfDoctrineApplyPlugin 1.2.2</a></li><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfDoctrineApplyPlugin/1_2_1'>sfDoctrineApplyPlugin 1.2.1</a></li></ul></aside><aside id="text-4" class="widget widget_text"><h3 class="widget-title">Mon twitter</h3>			<div class="textwidget"><div id="twitter_div" class="linkcat">
<ul id="twitter_update_list"></ul>
<a href="http://twitter.com/cedriclombardot" id="twitter-link" style="display:block;text-align:right;">Retrouvez moi sur Twitter</a>
</div>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/cedriclombardot.json?callback=twitterCallback2&count=5"></script></div>
		</aside><aside id="text-5" class="widget widget_text"><h3 class="widget-title">Contactez-moi</h3>			<div class="textwidget">  <img src="http://0.gravatar.com/avatar/e33e3b0f5cd521f7f9e0ee1096c3d0ed?s=70&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D30&r=G" style="float:right"/>  <b>Cédric Lombardot</b><br>
        44 Avenue de saint Ouen<br>
        75 018 PARIS<br>
         
          <ul class="contact">
            <li> <img height="16" width="16" alt="phone" src="http://lombardot.fr/squelettes/images/phone.png"> : 06 01 91 57 40 </li>
            <li> <img alt="email" src="http://lombardot.fr/squelettes/images/email.png"> : <a href="http://lombardot.fr/spip.php?page=contact">cedric.lombardot@gmail.com</a> </li>
            <li> <img alt="viadeo" src="http://lombardot.fr/squelettes/images/viadeo-logo.png"> : <a target="_blank" href="http://www.viadeo.com/fr/profile/cedric.lombardot">Mon Profil viadeo</a> </li>
          	<li> <img alt="blog" src="http://lombardot.fr/squelettes/images/kblogger.png"> : <a target="_blank" href="http://cedric.lombardot.fr">Mon CV</a> </li>

<li> <img alt="blog" src="http://lombardot.fr/squelettes/images/kblogger.png"> : <a target="_blank" href="http://symfony2admingenerator.org">Admingenerator symfony2</a> </li>
          </ul>

</div>
		</aside><aside id="text-6" class="widget widget_text">			<div class="textwidget"><script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4500211-3");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<style>
#text-6{ display:none }
</style>
</div>
		</aside>		</div><!-- #secondary -->
		</div><!-- #main .wrapper -->
	<footer id="colophon" role="contentinfo">
		<div class="site-info">
						<a href="http://wordpress.org/" title="Plate-forme de publication personnelle à la pointe de la sémantique">Fièrement propulsé par WordPress</a>
		</div><!-- .site-info -->
	</footer><!-- #colophon -->
</div><!-- #page -->

<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shCore.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushCSharp.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushPhp.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushJScript.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushJava.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushVb.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushSql.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushXml.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushDelphi.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushPython.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushRuby.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushCss.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushCpp.js"></script>
<script class="javascript">
dp.SyntaxHighlighter.ClipboardSwf = 'http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<script type='text/javascript' src='http://blog.lombardot.fr/wp-content/themes/twentytwelve/js/navigation.js?ver=1.0'></script>
</body>
</html>