...

View Full Version : Limit Character Output



bredge
12-05-2010, 07:09 PM
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 ??


<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/>
<?
}
?>

tropane
12-05-2010, 10:04 PM
this should work
<?

$characters=30; // Define how many character you want to display.

$post = substr($topic_title, 0, $characters);

echo $post;
echo "...";

?>

bredge
12-05-2010, 11:05 PM
So how would you incorporate that into my code ?

Lamped
12-06-2010, 12:03 AM
<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/>
<?
}
?>

sitNsmile
12-06-2010, 01:39 PM
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.


function _wTrim( $string , $limit , $too )
{
// cut the string
if ( strlen( $string ) > $limit )
{
$newString = substr( $string , 0 , $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)



<? $yourVariable = _wTrim($stringVariable,'10','...'); ?>

bredge
12-06-2010, 05:18 PM
Thanks Lamped that works a treat...

Heres my complete 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"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum