Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Limit Character Output

    I am using the below code to output the latest 4 topics from a forum. But does anyone know how I would limit the amount of characters from the post title that is output to the screen with say ....... after x amount of characters to represent that the title has been shortened ??

    Code:
    <div>
    <?
            
        foreach($topics as $t)
        {
            // Get folder img, topic status/type related information
            $topic_tracking_info = get_complete_topic_tracking($t['forum_id'], $t['topic_id']);
            $unread_topic = (isset($topic_tracking_info[$t['topic_id']]) && $t['topic_last_post_time'] > $topic_tracking_info[$t['topic_id']]) ? true : false;
            $folder_img = $folder_alt = $topic_type = '';
            topic_status($t, $t['topic_replies'], $unread_topic, $folder_img, $folder_alt, $topic_type);
    
            // output the link
            ?><img src="../pics/feed_arrow.jpg" align="middle">
            <title="<?=$user->lang[$folder_alt];?>" alt="?=$user->lang[$folder_alt];?>"/><a href="<?=$phpbb_root_path . 'viewtopic.php?f=' . $t['forum_id'] . '&amp;t=' . $t['topic_id'] . '&amp;p=' . $t['topic_last_post_id'] . '#p' . $t['topic_last_post_id'];?>" target="_parent"><?=html_entity_decode($t['topic_title']);?></a><br/>
    <?
        }
        ?>
    Last edited by bredge; 12-05-2010 at 06:43 PM.

  • #2
    New Coder
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    31
    Thanks
    0
    Thanked 9 Times in 8 Posts
    this should work
    PHP Code:
    <?

    $characters
    =30// Define how many character you want to display.
     
    $post substr($topic_title0$characters);

    echo 
    $post;
    echo 
    "..."

    ?>
    Last edited by tropane; 12-05-2010 at 09:07 PM.

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So how would you incorporate that into my code ?

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    <div>
    <?
            
        
    foreach($topics as $t)
        {
            
    // Get folder img, topic status/type related information
            
    $topic_tracking_info get_complete_topic_tracking($t['forum_id'], $t['topic_id']);
            
    $unread_topic = (isset($topic_tracking_info[$t['topic_id']]) && $t['topic_last_post_time'] > $topic_tracking_info[$t['topic_id']]) ? true false;
            
    $folder_img $folder_alt $topic_type '';
            
    topic_status($t$t['topic_replies'], $unread_topic$folder_img$folder_alt$topic_type);

    // Here:
    $topic_max 30;
    if (
    strlen($t['topic_title']) > $topic_max) {
        
    $t['topic_title'] = substr($t['topic_title'], 0$topic_max-3).'...';
    }

            
    // output the link
            
    ?><img src="../pics/feed_arrow.jpg" align="middle">
            <title="<?=$user->lang[$folder_alt];?>" alt="?=$user->lang[$folder_alt];?>"/><a href="<?=$phpbb_root_path 'viewtopic.php?f=' $t['forum_id'] . '&amp;t=' $t['topic_id'] . '&amp;p=' $t['topic_last_post_id'] . '#p' $t['topic_last_post_id'];?>" target="_parent"><?=html_entity_decode($t['topic_title']);?></a><br/>
    <?
        
    }
        
    ?>
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    If you want to trim more text in your script. heres a function, I use for my entire program. I dont care for replica code throughout the script. Globals insure security for future expansions.

    PHP Code:
    function _wTrim$string $limit $too )
    {
        
    // cut the string
        
    if ( strlen$string ) > $limit 
        {
            
    $newString substr$string $limit ) . $too;
        }
        else
        {
            
    $newString $string;
        }
            
        return 
    $newString;
            
    // end 
    All you would need to do is add this function to a global page or anywhere in your script so you can call it. Add that above function to your script. and where ever you like to trim some text. use the call below replacing ($stringVariable with the text variable you want to trim, and the limit of how long you want your string)

    PHP Code:
    <? $yourVariable _wTrim($stringVariable,'10','...'); ?>
    Last edited by sitNsmile; 12-06-2010 at 12:44 PM.

  • #6
    New Coder
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Lamped that works a treat...

    Heres my complete code..

    PHP Code:
    <style type="text/css">
    a:link {
        font-family: Tahoma;
        font-size: 10px;
        color: #FF0000;
        text-decoration: none;
        font-weight: normal;
            text-transform: uppercase;
    }
    a:active {
        font-family: Tahoma;
        font-size: 10px;
        color: #FF0000;
        text-decoration: none;
        font-weight: normal;
            text-transform: uppercase;
    }
    a:visited {
        font-family: Tahoma;
        font-size: 10px;
        color: #FF0000;
        text-decoration: none;
        font-weight: normal;
        text-transform: uppercase;
    }
    a:hover {
        font-family: Tahoma;
        font-size: 10px;
        color: #000000;
        text-decoration: none;
        font-weight: normal;
            text-transform: uppercase;

    }

    }
    text {
        font-family: Tahoma;
        font-size: 10px;
        color: #000000;
        text-decoration: none;
        font-weight: normal;
            text-transform: uppercase;

    }


    </style>
    <?
        define
    ('IN_PHPBB'true);
        
    $phpbb_root_path '../forum/'// Path to phpbb folder
        
    $phpEx substr(strrchr(__FILE__'.'), 1);
        include(
    $phpbb_root_path 'common.' $phpEx);
        include(
    $phpbb_root_path 'includes/functions_display.' $phpEx);

        
    // Start session management
        
    $user->session_begin();
        
    $auth->acl($user->data);

        
    // Grab user preferences
        
    $user->setup();
    ?>
    <?
        
    /*** phpBB3 - Last Active Topics System ***/
        //Show last x topics
        
    define('TOPICS_LIMIT',7);

        
    // Create arrays
        
    $topics = array();
        
        
    // Get forums that current user has read rights to.
        
    $forums array_unique(array_keys($auth->acl_getf('f_read'true)));
        
        
    // Get active topics.
        
    $sql="SELECT *
        FROM " 
    TOPICS_TABLE "
        WHERE topic_approved = '1' AND " 
    $db->sql_in_set('forum_id'$forums) . "
        ORDER BY topic_last_post_time DESC"
    ;
        
    $result $db->sql_query_limit($sql,TOPICS_LIMIT);
        while (
    $r $db->sql_fetchrow($result))
        {
            
    $topics[] = $r;
        }
       
    $db->sql_freeresult($result);
    ?>
    <div>
    <?
            
        
    foreach($topics as $t)
        {
            
    // Get folder img, topic status/type related information
            
    $topic_tracking_info get_complete_topic_tracking($t['forum_id'], $t['topic_id']);
            
    $unread_topic = (isset($topic_tracking_info[$t['topic_id']]) && $t['topic_last_post_time'] > $topic_tracking_info[$t['topic_id']]) ? true false;
            
    $folder_img $folder_alt $topic_type '';
            
    topic_status($t$t['topic_replies'], $unread_topic$folder_img$folder_alt$topic_type);

    // Here:
    $topic_max 29;
    if (
    strlen($t['topic_title']) > $topic_max) {
        
    $t['topic_title'] = substr($t['topic_title'], 0$topic_max-3).'...';
    }

            
    // output the link
            
    ?>    WANT TO PUT A - HERE
            <title="<?=$user->lang[$folder_alt];?>" alt="?=$user->lang[$folder_alt];?>"/><a href="<?=$phpbb_root_path 'viewtopic.php?f=' $t['forum_id'] . '&amp;t=' $t['topic_id'] . '&amp;p=' $t['topic_last_post_id'] . '#p' $t['topic_last_post_id'];?>" target="_parent"><?=html_entity_decode($t['topic_title']);?></a><br/>
            <?
        
    }
        
    ?>

    </div>
    But why is it everytime I want to put in a - next to each line the gap between each topics link doubles ? I've marking in the code where I want to put it with "WANT TO PUT A - HERE"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •