Forum Moderators: coopster & phranque

Message Too Old, No Replies

Breaking long words

breaking long words

         

johnnyman

8:15 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



I know this seems elementary but How do I use perl regular expressions to break a unbroken super long word? In my database that users input, I sometimes come across gibberish thats way too long and would cause my screen to have to scroll far to the right when i display it.

I dont want to break by # of characters alone but by # of characters in a word.

like if a word is more than 30 chr long then, insert a dash and space after every 30th letter only in that word. if any words are not more than 30 chr. long then leave alone

i.e. "check out supercaliblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah and all other cool stuff"

to: "check out supercaliblahblahblahblahblahb- lahblahblahblahblahblahblahbla- hblahblahblahblah and all other cool stuff"

this space is so it will naturally wrap around.

If I cant do it in with a regex can i do it in an array?

I've tried for two weeks to no avail i keep inserting every 30 chr. regardless of words :(

any help would be appreciated!
John

bennymack

8:34 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



cat >> test.pl
#!/usr/bin/perl -w

my $foo = q[supercaliblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah];
print $foo."\n";

$foo =~ s¦(\w{30})¦$1- ¦g;

print $foo."\n";
[CTRL+C]
./test.pl
supercaliblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah
supercaliblahblahblahblahblahb- lahblahblahblahblahblahblahbla- hblahblahblahblah

johnnyman

9:07 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



Thanks so much! i can start going to bed before 4am now lol