PHP Forum Topic Character Limit

Hello everyone. I have tried and tried to fix this problem, but I am not well enough versed in PHP to do it myself. What this PHP code is doing is grabbing the last topic from my forum and displaying it on my main page. My problem is that some of the longer posts do not fit in the area designated for this.

What I would like is a topic limit of about 50 characters and then followed by “…” Please keep in mind that $limit is the limit of the text displayed when your cursor is over the link.

Here is the coding:

[php]<?php
/*

  • Plugin Name: Last phpbb3 topics
  • Plugin URI: http://www.gekkotaku.com/
  • Description: Plugin for display last phpbb3 topics.
  • Version: 1.8
  • Author: @netOrX
  • Author URI: http://www.twitter.com/netOrX
  • For display the results, put this in your template:
  • <?php lastPhpbbTopic('displays=8&location=1&dir=http://YOUR_FORUM_ADDRESS.com/&table_prefix=YOUR_DB_TABLE_PREFIX'); ?>
  • VERY IMPORTANT
  • if you forum and the wordpress was in the same DataBase use 1
  • if the forum are in different DataBase use 0, and fill your DataBase information
    */

/**

  • @param array $args The argument array

  • @return list of topics
    */
    function lastPhpbbTopic($args = ‘’) {

    $defaults = array(
    ‘location’ => 0,
    ‘displays’ => 3,
    ‘before’ => ‘’,
    ‘after’ => ‘

    ’,
    ‘table_prefix’ => ‘phpbb_’, // this is your forum DB table prefix, CHANGE BY YOUR OWN
    ‘limit’ => 40,
    ‘dir’ => ‘*****’ // this is your forum address, CHANGE BY YOUR OWN
    );

    $r = wp_parse_args( $args, $defaults );
    extract($r, EXTR_SKIP);

    // follow me for exclude your hidden or private forums…
    // if you dont know what ID have your hidden forums:
    // * go to your hidden forum
    // * check your direction bar (in your navigator, EX. firefox, explorer)
    // * put atention on this~ viewforum.php?f=8
    // f - means forum, in this case, my hidden forum is the id - 8
    $hidden_forum = “12,33,36”; // separate your forum ID with ,

    if ( $location ) {
    global $wpdb;
    } else { // CONFIGURE THIS IF YOUR DATABASE FOURM ARE IN DIFFERENT PLACE THAN WORDPRESS
    // fill with your db acces information
    $DB_HOST = ‘’; // host (localhost, 06.mysqlprovider.com)
    $DB_NAME = ‘’; // your data base name
    $DB_USER = '
    ’; // your data base user name
    $DB_PASS = ‘*****’; // your data base password

      $wpdb = new wpdb($DB_USER, $DB_PASS, $DB_NAME, $DB_HOST);
    

    }

    // IF YOU DONT HAVE EXPERIENCE… DONT TOUCH THIS
    $sql = "SELECT
    forum.forum_name, post.forum_id, post.post_text,
    topic.topic_id, topic.topic_title, topic.topic_replies,
    topic.topic_last_post_id, user.username
    FROM " . $table_prefix . "posts post
    LEFT JOIN " . $table_prefix . "forums forum ON post.forum_id = forum.forum_id
    LEFT JOIN " . $table_prefix . “users user ON post.poster_id = user.user_id
    LEFT JOIN " . $table_prefix . “topics topic ON post.topic_id = topic.topic_id
    WHERE post.forum_id NOT IN (”.$hidden_forum.”)
    ORDER BY post.post_time DESC LIMIT $displays
    ";

    $result = $wpdb->get_results($sql);

    if ( !$result ) { die('Invalid query: ’ . mysql_error()); }

foreach ($result as $topic) {
$start = round($topic->topic_replies / 10).‘0’;
$topic->post_text = preg_replace("([.+?])is",’’,$topic->post_text);
$topic->post_text = substr($topic->post_text,0,$limit)."…";

echo $before.’ ‘.$topic->topic_title.’’.$after;

}

}
?>[/php]

Any help if MUCH appreciated!

Ok, so i hope i understand your question right, let me know if i answered the wrong question.

This line is doing the truncation for you:
[php]
$topic->post_text = substr($topic->post_text,0,$limit)."…";
[/php]

The $limit is set in the array at the top of the script:
[php]
$defaults = array(
‘location’ => 0,
‘displays’ => 3,
‘before’ => ‘’,
‘after’ => ‘

’,
‘table_prefix’ => ‘phpbb_’,
‘limit’ => 40, // <---- RIGHT HERE
‘dir’ => ‘*****’
);
[/php]

It’s set to a limit of 40, so, to answer your question just change that value to a 50.

Read your question again and noticed that you were asking how to include it in the little tooltip thing when you hover. ::slight_smile: Take a look at the modified code below. You can adjust the ‘title_limit’ to try different lengths.

[php]

<?php /* * Plugin Name: Last phpbb3 topics * Plugin URI: http://www.gekkotaku.com/ * Description: Plugin for display last phpbb3 topics. * Version: 1.8 * Author: @netOrX * Author URI: http://www.twitter.com/netOrX * * * For display the results, put this in your template: * <?php lastPhpbbTopic('displays=8&location=1&dir=http://YOUR_FORUM_ADDRESS.com/&table_prefix=YOUR_DB_TABLE_PREFIX'); ?>
  • VERY IMPORTANT
  • if you forum and the wordpress was in the same DataBase use 1
  • if the forum are in different DataBase use 0, and fill your DataBase information
    */

/**

  • @param array $args The argument array

  • @return list of topics
    */
    function lastPhpbbTopic($args = ‘’) {

    $defaults = array(
    ‘location’ => 0,
    ‘displays’ => 3,
    ‘before’ => ‘’,
    ‘after’ => ‘

    ’,
    ‘table_prefix’ => ‘phpbb_’, // this is your forum DB table prefix, CHANGE BY YOUR OWN
    ‘limit’ => 40,
    ‘title_limit’ => 50, // Max length of the tooltip text
    ‘dir’ => ‘*****’ // this is your forum address, CHANGE BY YOUR OWN
    );

    $r = wp_parse_args( $args, $defaults );
    extract($r, EXTR_SKIP);

    // follow me for exclude your hidden or private forums…
    // if you dont know what ID have your hidden forums:
    // * go to your hidden forum
    // * check your direction bar (in your navigator, EX. firefox, explorer)
    // * put atention on this~ viewforum.php?f=8
    // f - means forum, in this case, my hidden forum is the id - 8
    $hidden_forum = “12,33,36”; // separate your forum ID with ,

    if ( $location ) {
    global $wpdb;
    } else { // CONFIGURE THIS IF YOUR DATABASE FOURM ARE IN DIFFERENT PLACE THAN WORDPRESS
    // fill with your db acces information
    $DB_HOST = ‘’; // host (localhost, 06.mysqlprovider.com)
    $DB_NAME = ‘’; // your data base name
    $DB_USER = '
    ’; // your data base user name
    $DB_PASS = ‘*****’; // your data base password

    $wpdb = new wpdb($DB_USER, $DB_PASS, $DB_NAME, $DB_HOST);
    

    }

    // IF YOU DONT HAVE EXPERIENCE… DONT TOUCH THIS
    $sql = "SELECT
    forum.forum_name, post.forum_id, post.post_text,
    topic.topic_id, topic.topic_title, topic.topic_replies,
    topic.topic_last_post_id, user.username
    FROM " . $table_prefix . "posts post
    LEFT JOIN " . $table_prefix . "forums forum ON post.forum_id = forum.forum_id
    LEFT JOIN " . $table_prefix . “users user ON post.poster_id = user.user_id
    LEFT JOIN " . $table_prefix . “topics topic ON post.topic_id = topic.topic_id
    WHERE post.forum_id NOT IN (”.$hidden_forum.”)
    ORDER BY post.post_time DESC LIMIT $displays
    ";

    $result = $wpdb->get_results($sql);

    if ( !$result ) { die('Invalid query: ’ . mysql_error()); }

    foreach ($result as $topic) {
    $start = round($topic->topic_replies / 10).‘0’;
    $topic->post_text = preg_replace("([.+?])is",’’,$topic->post_text);

    // Create the title text, truncated by the title
    $titleText = substr($topic->post_text, 0, $title_limit)."...";
    
    // Create the post text
    $topic->post_text = substr($topic->post_text,0,$limit)."...";
    
    echo $before.'<a href="'.$dir.'viewtopic.php?f='.
         $topic->forum_id.'&t='.$topic->topic_id.'&start='.$start.'#p'.
         $topic->topic_last_post_id.'" title="'.$topic->post_text.'" target="_blank" title="'.
         $topic->post_text.'">        '.
         $topic->topic_title.'</a>'.$after;
    

    }
    }
    ?>
    [/php]

Man, i really wish they let you edit posts, that last one had a mistake try this:

[php]<?php
/*

  • Plugin Name: Last phpbb3 topics
  • Plugin URI: http://www.gekkotaku.com/
  • Description: Plugin for display last phpbb3 topics.
  • Version: 1.8
  • Author: @netOrX
  • Author URI: http://www.twitter.com/netOrX
  • For display the results, put this in your template:
  • <?php lastPhpbbTopic('displays=8&location=1&dir=http://YOUR_FORUM_ADDRESS.com/&table_prefix=YOUR_DB_TABLE_PREFIX'); ?>
  • VERY IMPORTANT
  • if you forum and the wordpress was in the same DataBase use 1
  • if the forum are in different DataBase use 0, and fill your DataBase information
    */

/**

  • @param array $args The argument array

  • @return list of topics
    */
    function lastPhpbbTopic($args = ‘’) {

    $defaults = array(
    ‘location’ => 0,
    ‘displays’ => 3,
    ‘before’ => ‘’,
    ‘after’ => ‘

    ’,
    ‘table_prefix’ => ‘phpbb_’, // this is your forum DB table prefix, CHANGE BY YOUR OWN
    ‘limit’ => 40,
    ‘title_limit’ => 50, // Max length of the tooltip text
    ‘dir’ => ‘*****’ // this is your forum address, CHANGE BY YOUR OWN
    );

    $r = wp_parse_args( $args, $defaults );
    extract($r, EXTR_SKIP);

    // follow me for exclude your hidden or private forums…
    // if you dont know what ID have your hidden forums:
    // * go to your hidden forum
    // * check your direction bar (in your navigator, EX. firefox, explorer)
    // * put atention on this~ viewforum.php?f=8
    // f - means forum, in this case, my hidden forum is the id - 8
    $hidden_forum = “12,33,36”; // separate your forum ID with ,

    if ( $location ) {
    global $wpdb;
    } else { // CONFIGURE THIS IF YOUR DATABASE FOURM ARE IN DIFFERENT PLACE THAN WORDPRESS
    // fill with your db acces information
    $DB_HOST = ‘’; // host (localhost, 06.mysqlprovider.com)
    $DB_NAME = ‘’; // your data base name
    $DB_USER = '
    ’; // your data base user name
    $DB_PASS = ‘*****’; // your data base password

    $wpdb = new wpdb($DB_USER, $DB_PASS, $DB_NAME, $DB_HOST);
    

    }

    // IF YOU DONT HAVE EXPERIENCE… DONT TOUCH THIS
    $sql = "SELECT
    forum.forum_name, post.forum_id, post.post_text,
    topic.topic_id, topic.topic_title, topic.topic_replies,
    topic.topic_last_post_id, user.username
    FROM " . $table_prefix . "posts post
    LEFT JOIN " . $table_prefix . "forums forum ON post.forum_id = forum.forum_id
    LEFT JOIN " . $table_prefix . “users user ON post.poster_id = user.user_id
    LEFT JOIN " . $table_prefix . “topics topic ON post.topic_id = topic.topic_id
    WHERE post.forum_id NOT IN (”.$hidden_forum.”)
    ORDER BY post.post_time DESC LIMIT $displays
    ";

    $result = $wpdb->get_results($sql);

    if ( !$result ) { die('Invalid query: ’ . mysql_error()); }

    foreach ($result as $topic) {
    $start = round($topic->topic_replies / 10).‘0’;
    $topic->post_text = preg_replace("([.+?])is",’’,$topic->post_text);

    // Create the title text, truncated by the title
    $titleText = substr($topic->post_text, 0, $title_limit)."...";
    
    // Create the post text
    $topic->post_text = substr($topic->post_text,0,$limit)."...";
    
    echo $before.'<a href="'.$dir.'viewtopic.php?f='.
         $topic->forum_id.'&t='.$topic->topic_id.'&start='.$start.'#p'.
         $topic->topic_last_post_id.'" title="'.$topic->post_text.'" target="_blank" title="'.
         $titleText.'">        '.
         $topic->topic_title.'</a>'.$after;
    

    }
    }
    ?>[/php]

Hello Aaron, I apologize I was so vague, I was in a hurry. What I am looking for is to limit the length of the title itself. The $limit limits the text shown on mouse hover from the forum body. I have attached a picture…

I need the pink circle part limited and then truncation

but in this script the $limit limits the green arrow.

I hope this is much clearer. Thank you for your help thus far.

Oooooh, that makes more sense. This should do it. I added a topic_limit and then truncate it to 50 chars and add a ‘…’ if it’s too long.

[php]

<?php /* * Plugin Name: Last phpbb3 topics * Plugin URI: http://www.gekkotaku.com/ * Description: Plugin for display last phpbb3 topics. * Version: 1.8 * Author: @netOrX * Author URI: http://www.twitter.com/netOrX * * * For display the results, put this in your template: * <?php lastPhpbbTopic('displays=8&location=1&dir=http://YOUR_FORUM_ADDRESS.com/&table_prefix=YOUR_DB_TABLE_PREFIX'); ?>
  • VERY IMPORTANT
  • if you forum and the wordpress was in the same DataBase use 1
  • if the forum are in different DataBase use 0, and fill your DataBase information
    */

/**

  • @param array $args The argument array

  • @return list of topics
    */
    function lastPhpbbTopic($args = ‘’) {
    $defaults = array(‘location’ => 0,
    ‘displays’ => 3,
    ‘before’ => ‘’,
    ‘after’ => ‘

    ’,
    ‘table_prefix’ => ‘phpbb_’, // this is your forum DB table prefix, CHANGE BY YOUR OWN
    ‘limit’ => 40,
    ‘topic_limit’ => 50,
    ‘dir’ => ‘*****’ // this is your forum address, CHANGE BY YOUR OWN
    );

    $r = wp_parse_args( $args, $defaults );

    extract($r, EXTR_SKIP);

    // follow me for exclude your hidden or private forums…
    // if you dont know what ID have your hidden forums:
    // * go to your hidden forum
    // * check your direction bar (in your navigator, EX. firefox, explorer)
    // * put atention on this~ viewforum.php?f=8
    // f - means forum, in this case, my hidden forum is the id - 8
    $hidden_forum = “12,33,36”; // separate your forum ID with ,

    if ( $location ) {
    global $wpdb;
    } else { // CONFIGURE THIS IF YOUR DATABASE FOURM ARE IN DIFFERENT PLACE THAN WORDPRESS
    // fill with your db acces information
    $DB_HOST = ‘’; // host (localhost, 06.mysqlprovider.com)
    $DB_NAME = ‘’; // your data base name
    $DB_USER = '
    ’; // your data base user name
    $DB_PASS = ‘*****’; // your data base password
    $wpdb = new wpdb($DB_USER, $DB_PASS, $DB_NAME, $DB_HOST);
    }

    // IF YOU DONT HAVE EXPERIENCE… DONT TOUCH THIS
    $sql = "SELECT forum.forum_name, post.forum_id, post.post_text, topic.topic_id, topic.topic_title, topic.topic_replies,topic.topic_last_post_id, user.username FROM "
    . $table_prefix . "posts post LEFT JOIN "
    . $table_prefix . "forums forum ON post.forum_id = forum.forum_id LEFT JOIN "
    . $table_prefix . “topics topic ON post.topic_id = topic.topic_id WHERE post.forum_id NOT IN (”
    . $hidden_forum . “) ORDER BY post.post_time DESC LIMIT $displays”;

    $result = $wpdb->get_results($sql);

    if ( !$result ) { die('Invalid query: ’ . mysql_error()); }

    foreach ($result as $topic)
    {
    $start = round($topic->topic_replies / 10).‘0’;
    $topic->post_text = preg_replace("([.+?])is",’’,$topic->post_text);
    $topic->post_text = substr($topic->post_text,0,$limit)."…";

      // Truncate the topic title if it's too long
      if(strlen($topic->topic_title) > $topicLimit)
      	$topic->topic_title = substr($topic->topic_title, 0, $topicLimit) . "...";
    
      echo $before . '<a href="' . $dir . 'viewtopic.php?f=' 
      	. $topic->forum_id.'&t=' . $topic->topic_id . '&start=' 
      	. $start . '#p' . $topic->topic_last_post_id . '" title="' 
      	. $topic->post_text . '" target="_blank">        ' 
      	. $topic->topic_title . '</a>' . $after;
    

    }
    }
    ?>
    [/php]

Cheers!

Sponsor our Newsletter | Privacy Policy | Terms of Service