Yeah - I use the code below, which I made to limit title length for news stories. The main advantage is it limits to specified width, but then *backtracks* to the last whitespace before that in the string, so you get whole words output followed by '...' :-)


===============================================


// Title width limiter

$titlelimit=50; // Number of characters to allow in a news title subsection (narrow column)

if (strlen($row->newstitle)<$titlelimit) {
$secvars["news_title"]=htmlspecialchars($row->newstitle);
} else {
$limitedstring=substr($row->newstitle,0,$titlelimit);
$lastspace=strrpos($limitedstring," ");
if ($lastspace===false || $lastspace==$titlelimit) {
// Theres no space to chop string on, or its at the last position : just limit string to substring
$secvars["news_title"]=htmlspecialchars($limitedstring)."&nbsp;...";
} else {
// Found a space near end of the string, back off to last space found and add ellipsis
$secvars["news_title"]=htmlspecialchars(substr($limitedstring,0,$lastspace))."&nbsp;...";
}
}
// End title width limiter



================================== Hope that's (obvious !) , Regards, Neil Smith.

At 03:03 30/03/2003 +0000, you wrote:
Message-Id: <[EMAIL PROTECTED]>
Date: Sat, 29 Mar 2003 13:27:00 +0200
From: "Kobus Myburgh" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Subject: Re: [PHP-WIN] Tables (might be OT)

Hi,

I have done a similar thing that worked reasonably well in Visual Basic (however, included is a PHP snippet which might do the trick - I am not a PHP pro).

write a small PHP function to select only a part of the string, say the first 15 characters, and append "..." to the string, for example,

thisisoneveryveryveryveryverylongword!!!!

will become:

thisisoneveryver...

This is standard PHP string manipulation, which might look something like this:


function myTruncate ($verylongstring, $length) { if (strlen($verylongstring) > $length) { $shorterstring = substr($verylongstring,0,$length-1) . "..."; } else $shorterstring = $verylongstring; } return $shorterstring; }


--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to