[FIXED] Include partial message in <description>

Support for the phpBB2 SEO mods released in the phpBB2 SEO Toolikt forum.

Moderator: Moderators

[FIXED] Include partial message in <description>

Postby dr_somm » Mon Mar 02, 2009 2:39 am

Is it possible to include the first part of a message in <meta name="description"> on viewtopic.php like with the dynamic meta tags MOD for phpBB 3?
Last edited by dr_somm on Sat Mar 07, 2009 8:20 pm, edited 1 time in total.
dr_somm
 
Posts: 29
Joined: Sun Mar 01, 2009 9:27 pm

Advertisement

Re: [Dynamic Meta Tags] Include partial message in <description>

Postby dr_somm » Mon Mar 02, 2009 7:36 pm

Although I know approx. nothing about PHP, I tried to modify the dynamic meta tag Mod for phpBB2 using stuff from the new phpBB3 Mod:

This is what my phpbb_seo_class.php looks like now:
#1
Code: Select all
(...)

   function make_keywords($text) {
      global $phpbb_root_path;
      static $stop_words = array();
      $keywords = '';
      $num = 0;
      $text = strtolower(preg_replace(array('`&(amp;)?#?[a-z0-9]+;`i', '`[[:punct:]]+`', '`[0-9]+`',  '`[\s]+`'), ' ', strip_tags($text)));
      $text = explode(' ', trim($text), 50);
      // I'm using a "search_stopwords.php" like in phpBB3
      include($phpbb_root_path . 'language/lang_german/search_stopwords.php');
      $stop_words = & $words;
      $text = array_diff($text, $stop_words);
      // We take the most used words first
      $text = array_count_values($text);
      arsort($text);
      foreach ($text as $word => $count) {
         if ( strlen($word) >= 3 ) {
            $keywords .= ', ' . $word;
            $num++;
            if ( $num >= 15 ) {   
               break;
            }
         }   
      }
      return trim($keywords, ', ');
   }
   /**
   * Filter php/html tags and white spaces and returns htmlspecialchared string with limit in words
   */
   function meta_filter_txt($text, $bbcode = true) {
      if ($bbcode) {
         static $RegEx = array();
         static $replace = array(' ', ' ', '', ' ');
         if (empty($RegEx)) {
            $RegEx = array('`<[^>]*>(.*<[^>]*>)?`Usi', // HTML code
               '`\[(' . 'img|url|flash|code' . ')[^\[\]]+\].*\[/\1[^\[\]]+\]`Usi', // bbcode to strip
               '`\[/?[^\[\]]+\]`mi', // Strip all bbcode tags
               '`[\s]+`' // Multiple spaces
            );
         }
         return $this->word_limit(htmlspecialchars(preg_replace($RegEx, $replace, $text), ENT_COMPAT));
      }
      return $this->word_limit(htmlspecialchars(preg_replace(array('`<[^>]*>(.*<[^>]*>)?`Usi', '`[\s]+`'), ' ', $text), ENT_COMPAT));
   }
   /**
   * Cut the text according to the number of words.
   * Borrowed from www.php.net http://www.php.net/preg_replace
   */
   function word_limit($string) {
      return count($words = preg_split('/\s+/', ltrim($string), 25 + 1)) > 25 ? rtrim(substr($string, 0, strlen($string) - strlen(end($words)))) . '...' : $string;
   }
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
(...)


I also changed something in viewtopic.php:
#2
Code: Select all
// www.phpBB-SEO.com SEO TOOLKIT BEGIN - META
$phpbb_seo->seo_meta['meta_desc'] = $phpbb_seo->meta_filter_txt($postrow[0]['post_text']);
$m_kewrd = '';
$sql = "SELECT w.word_text
      FROM " . TOPICS_TABLE . " t, " . SEARCH_MATCH_TABLE . " m, " . SEARCH_WORD_TABLE . " w
      WHERE t.topic_id = $topic_id
         AND t.topic_first_post_id = m.post_id
         AND m.word_id = w.word_id LIMIT 15";
if( ($result = $db->sql_query($sql)) ) {

   while ( $meta_row = $db->sql_fetchrow($result) ) {
      $m_kewrd .= " " . $meta_row['word_text'];
   }
}
$phpbb_seo->seo_meta['keywords'] = $phpbb_seo->make_keywords("$m_kewrd " . $phpbb_seo->seo_meta['meta_desc']);
// www.phpBB-SEO.com SEO TOOLKIT END - META


Does it make any difference if I'll change #2 into something like this?
Code: Select all
// www.phpBB-SEO.com SEO TOOLKIT BEGIN - META
$phpbb_seo->seo_meta['meta_desc'] = $phpbb_seo->meta_filter_txt($postrow[0]['post_text']);
$m_kewrd = '';
         $common_sql = true ? '' : 'AND w.word_common = 0';
         $sql = "SELECT w.word_text
            FROM " . SEARCH_MATCH_TABLE . " m, " . SEARCH_WORD_TABLE . " w
            WHERE m.post_id = " . (int) $row['post_id'] . "
               AND w.word_id = m.word_id LIMIT 15
               $common_sql
            ORDER BY w.word_count DESC";
         $result = $db->sql_query($sql);
         while ( $meta_row = $db->sql_fetchrow($result) ) {
            $m_kewrd .= ' ' . $meta_row['word_text'];
         }
         $db->sql_freeresult($result);
      $phpbb_seo->seo_meta['keywords'] = !empty($m_kewrd) ? $phpbb_seo->make_keywords($m_kewrd) : $phpbb_seo->make_keywords($phpbb_seo->seo_meta['meta_desc']);
// www.phpBB-SEO.com SEO TOOLKIT END - META


Anyway, everything seems to work fine now. The only thing that still bothers me is stuff like &amp; in my meta tags :/

I'd highly appreciate any comment on this crappy copy&paste-code - thanks! ;)
dr_somm
 
Posts: 29
Joined: Sun Mar 01, 2009 9:27 pm

Re: [Dynamic Meta Tags] Include partial message in <description>

Postby SeO » Sat Mar 07, 2009 11:27 am

You could use :
Code: Select all
         return $this->word_limit(htmlspecialchars(preg_replace($RegEx, $replace, htmlspecialchars_decode($text)), ENT_COMPAT));

Isntead of :
Code: Select all
         return $this->word_limit(htmlspecialchars(preg_replace($RegEx, $replace, $text), ENT_COMPAT));

But only if using PHP 5 >= 5.1.0.
SeO
Admin
Admin
 
Posts: 6333
Joined: Wed Mar 15, 2006 9:41 pm

Re: [Dynamic Meta Tags] Include partial message in <description>

Postby dr_somm » Sat Mar 07, 2009 5:51 pm

Thanks a lot - no more
Code: Select all
&amp;quot;hi&amp;quot;
meta tags!
dr_somm
 
Posts: 29
Joined: Sun Mar 01, 2009 9:27 pm


Return to phpBB2 SEO MODS

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest