Customizing SMF > SMF Coding Discussion

Shorten topic subjects displayed with ssi_boardNews?

(1/1)

Hragged:
Is there a way to set a character limit to topic subjects displayed with ssi_boardnews, so that if a topic subject is quite long part of it can be cut off or replaced with ... or something?

Thanks for any help!

Suki:
Yes, you can return the results as an array instead of printing the default layout with the las parameter on the function:

$myArray = ssi_boardNews($board = null, $limit = null, $start = null, $length = null, $output_method = 'return')

Then $myArray['subject'] contains the subject, now you can use

$amount =  strlen($myArray['subject']);

if ($amount >= 50)
$myArray['subject'] = 'something else replacing the original subject';

Keep in mind that this will replace the entire subject rather than adding ... at the end, to cut off a string I use this function:


--- Code: --- /* A function to cut-off a string */
public function truncateString($string, $limit, $break = ' ', $pad = '...')
{
if(empty($limit))
$limit = 30;

/* return with no change if string is shorter than $limit */
if(strlen($string) <= $limit)
return $string;

/* is $break present between $limit and the end of the string? */
if(false !== ($breakpoint = strpos($string, $break, $limit)))
if($breakpoint < strlen($string) - 1)
$string = substr($string, 0, $breakpoint) . $pad;

return $string;
}

--- End code ---

Navigation

[0] Message Index

Go to full version