CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Truncating data

 



benn600
User


Apr 16, 2005, 9:16 AM

Post #1 of 3 (994 views)
Truncating data Can't Post

How can I have a message, such as in a message board, truncated to the first 4-6 words or X characters, whichever is smaller. If a single string is used with no spaces, it could destroy the table with a huge string. I have searched around but have been unable to find much on this subject.
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


KevinR
Veteran


Apr 16, 2005, 11:23 AM

Post #2 of 3 (993 views)
Re: [benn600] Truncating data [In reply to] Can't Post

use the substr() and length() functions. Here is one way:


Code
my $string = "thisisalongcontinuousstringofwords"; 
my $n = length($string);
substr($string,20,$n,'...') if ($n>20);
print $string;


If the string is longer than 20 characters, everything after 20 characters is removed and replaced with "...". Adjust to your needs. You might also want to consider inserting a line break instead of truncating, or using overflow in your html tables.

<table style="overflow: auto;">
-------------------------------------------------


davorg
Thaumaturge / Moderator

Apr 17, 2005, 10:02 AM

Post #3 of 3 (990 views)
Re: [benn600] Truncating data [In reply to] Can't Post

The Text::Wrap module is a standard part of the Perl distribution. It reformats text to insert linebreaks at word breaks so that no line is more than X character long. You could use that and then just grab the first line.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives