News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Shorten a link/sentence with "..."

Started by Disoriented, November 13, 2010, 05:43:40 PM

Previous topic - Next topic

Disoriented

Hey. I'm using <?php ssi_recentTopics(); ?> on my index page. But I have a problem with the links to the recent topics being too long. If the name on the topic is more than 25 letters or so it goes out on to another row, and I don't want that, I want it to end in three little dots "..." after approx 25 letters or so.  I know that it's $post['subject'] that has to be "limited" but I have no idea how or what I should put where.

Here is the code for <?php ssi_recentTopics(); ?>.


// Recent topic list:   [board] Subject by Poster Date
function ssi_recentTopics($num_recent = 8, $exclude_boards = null, $output_method = 'echo')
{
global $context, $settings, $scripturl, $txt, $db_prefix, $ID_MEMBER;
global $user_info, $modSettings, $func;

if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
$exclude_boards = array($modSettings['recycle_board']);
else
$exclude_boards = empty($exclude_boards) ? array() : $exclude_boards;

$stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'moved', 'recycled', 'wireless');
$icon_sources = array();
foreach ($stable_icons as $icon)
$icon_sources[$icon] = 'images_url';

// Find all the posts in distinct topics.  Newer ones will have higher IDs.
$request = db_query("
SELECT
m.posterTime, ms.subject, m.ID_TOPIC, m.ID_MEMBER, m.ID_MSG, b.ID_BOARD, b.name AS bName,
IFNULL(mem.realName, m.posterName) AS posterName, " . ($user_info['is_guest'] ? '1 AS isRead, 0 AS new_from' : '
IFNULL(lt.ID_MSG, IFNULL(lmr.ID_MSG, 0)) >= m.ID_MSG_MODIFIED AS isRead,
IFNULL(lt.ID_MSG, IFNULL(lmr.ID_MSG, -1)) + 1 AS new_from') . ", LEFT(m.body, 384) AS body, m.smileysEnabled, m.icon
FROM ({$db_prefix}messages AS m, {$db_prefix}topics AS t, {$db_prefix}boards AS b, {$db_prefix}messages AS ms)
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)" . (!$user_info['is_guest'] ? "
LEFT JOIN {$db_prefix}log_topics AS lt ON (lt.ID_TOPIC = t.ID_TOPIC AND lt.ID_MEMBER = $ID_MEMBER)
LEFT JOIN {$db_prefix}log_mark_read AS lmr ON (lmr.ID_BOARD = b.ID_BOARD AND lmr.ID_MEMBER = $ID_MEMBER)" : '') . "
WHERE t.ID_LAST_MSG >= " . ($modSettings['maxMsgID'] - 35 * min($num_recent, 5)) . "
AND t.ID_LAST_MSG = m.ID_MSG
AND b.ID_BOARD = t.ID_BOARD" . (empty($exclude_boards) ? '' : "
AND b.ID_BOARD NOT IN (" . implode(', ', $exclude_boards) . ")") . "
AND $user_info[query_see_board]
AND ms.ID_MSG = t.ID_FIRST_MSG
ORDER BY t.ID_LAST_MSG DESC
LIMIT $num_recent", __FILE__, __LINE__);
$posts = array();
while ($row = mysql_fetch_assoc($request))
{
$row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']), array('<br />' => '
')));
if ($func['strlen']($row['body']) > 128)
$row['body'] = $func['substr']($row['body'], 0, 128) . '...';

// Censor the subject.
censorText($row['subject']);
censorText($row['body']);

if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';

// Build the array.
$posts[] = array(
'board' => array(
'id' => $row['ID_BOARD'],
'name' => $row['bName'],
'href' => $scripturl . '?board=' . $row['ID_BOARD'] . '.0',
'link' => '<a href="' . $scripturl . '?board=' . $row['ID_BOARD'] . '.0">' . $row['bName'] . '</a>'
),
'topic' => $row['ID_TOPIC'],
'poster' => array(
'id' => $row['ID_MEMBER'],
'name' => $row['posterName'],
),
'subject' => $row['subject'],
'short_subject' => shorten_subject($row['subject'], 25),
'preview' => $row['body'],
'time' => timeformat($row['posterTime']),
'timestamp' => forum_time(true, $row['posterTime']),
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.msg' . $row['ID_MSG'] . ';topicseen#new',
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.msg' . $row['ID_MSG'] . '#new">' . $row['subject'] . '</a>',
'new' => !empty($row['isRead']),
'new_from' => $row['new_from'],
'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />',
);
}
mysql_free_result($request);

// Just return it.
if ($output_method != 'echo' || empty($posts))
return $posts;

echo '
<table border="0" class="ssi_table">';
foreach ($posts as $post)
echo '
<tr>
<td valign="top" width="70%">
<a href="', $post['href'], '">', $post['subject'], '</a>
', $post['poster']['link'], '
', $post['new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new"><img src="' . $settings['images_url'] . '/' . $context['user']['language'] . '/new.gif" alt="' . $txt[302] . '" border="0" /></a>', '
</td>
<td align="right" nowrap="nowrap">
', $post['time'], '
</td>
</tr>';
echo '
</table>';
}



And this is how it looks in the HTML.


<table border="0" class="ssi_table">
<tr>
<td valign="top" width="70%">
<a href="http://fff/forum/index.php?topic=3.msg3;topicseen#new">asdasdasd</a>


</td>
<td align="right" nowrap="nowrap">
11 Nov, 2010, 12:51
</td>
</tr>
</table>


Please help :)

chilly

don't modify the function!
let the recent topics be passed as an array and make your own output. on the link you could use substr
http://www.php.net/manual/en/function.substr.php

Language Coordinator

Disoriented

Quote from: chilly on November 14, 2010, 12:23:17 PM
don't modify the function!
let the recent topics be passed as an array and make your own output. on the link you could use substr
hxxp:www.php.net/manual/en/function.substr.php [nonactive]

I'm actually quite new at this with php and the only thing I can do remove, change things a little. So I didn't really understand anything what you worte, nor anything in that manual. Think you could help me implant the "array" for me? :>

MrPhil

Someone else is looking for a "teaser" function: http://www.simplemachines.org/community/index.php?topic=409220.0 Is there such a thing already written? This might be easier than in the other topic (full post --> teaser), as presumably there are no embedded HTML or BBCode tags in the text. A general teaser function would be something like
$teaser_text = teaser($original_text, $max_length, $fraction, $insert);
where text is shortened to the nearest full word that is the shorter of fraction*length and max_length, and BBCode/HTML tags are ignored, and 'insert' text is added if the text is shortened (e.g., '...<a href="$link">(more)</a>'). There might also be a flag somewhere indicating whether the original text ended up being shortened.

In the given example, the topic title shortening would be
$new_title = teaser($full_text, 25, 1.0, '...');
Anyway, does this already exist? A general function would have to skip over HTML tags and make sure they end up correctly closed after shortening. It should also not lengthen the text (e.g., if the original is 26 characters, you don't want to trim it to 25 and then add 3).

Advertisement: