Re: Re: [PHP-WIN] Tables (might be OT)
| From: | Neil Smith | Date: | Sun, 30 Mar 2003 13:12:54 +0000 |
| Subject: | Re: Re: [PHP-WIN] Tables (might be OT) | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-19188@lists.php.net to get a copy of this message | ||
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)." ...";
} 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))." ...";
}
}
// End title width limiter
==================================
Hope that's (obvious !) , Regards,
Neil Smith.
At 03:03 30/03/2003 +0000, you wrote:
Message-Id: <se859f2b.066@groupwise.puk.ac.za> Date: Sat, 29 Mar 2003 13:27:00 +0200 From: "Kobus Myburgh" <ITBJDM@puknet.puk.ac.za> To: <dev@elstudion.com> Cc: <php-windows@lists.php.net> 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; }