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: Beginner:
substring

 



uatt
User

Jul 31, 2002, 6:35 AM

Post #1 of 4 (785 views)
substring Can't Post


Code
$len =length($string);  
$break = int($len/80);
for($i=0; $i<=$break; $i++)
{$result .= substr($string, $i*80, 80)."<br>\n";}



the code is to break long sentence of message board with break<br>(i.e in every 80 characters interval....

However, the meaning of the sentence may be destorted with an occurence of a complete word or a hyperlink like http:/something.com or something@some.com

so, I think we may shift the 80 a little bit backward when we met \b or http

Anyone can teach me how to do this as I am just stuck in this part.

thanks..


davorg
Thaumaturge / Moderator

Aug 1, 2002, 3:03 AM

Post #2 of 4 (780 views)
Re: [uatt] substring [In reply to] Can't Post

The Text::Wrap module (a standard part of Perl) will intelligently wrap text to a given width. You might find that useful.

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


uatt
User

Aug 1, 2002, 4:02 PM

Post #3 of 4 (777 views)
Re: [uatt] substring [In reply to] Can't Post

see and thanks...


Paul
Enthusiast

Aug 1, 2002, 6:54 PM

Post #4 of 4 (777 views)
Re: [uatt] substring [In reply to] Can't Post

Bear in mind this was rustled up at 3am so won't be fool proof but it gives you an idea I hope....


Code
my $str   = 'Hello, this is a long string that needs breaking into chunks of...hm...I\'d say 10 characters but of course we don\'t want to break words or hyperlinks etc.'; 
my $chunk = 10;

$str =~ s/(.{$chunk}[^\s\b]*)/$1<br>/g;

print $str;



(This post was edited by RedRum on Aug 1, 2002, 6:55 PM)

 
 


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

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