Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Splitting A Word?

by mpegman (Sexton)
on Jul 06, 2001 at 22:53 UTC ( #94550=perlquestion: print w/replies, xml ) Need Help??

mpegman has asked for the wisdom of the Perl Monks concerning the following question:

I am fairly new to perl and have run into a problem. On my site i have a message board simular to the chatterbox here, were they can post their thoughts. Sometimes the user post a very long word which skrews up the tables. I was wondering if it is possible to split a word into 30 character segments if it is longer than 30 characters. Thanks in advance.

Replies are listed 'Best First'.
Re: Splitting A Word?
by suaveant (Parson) on Jul 06, 2001 at 23:00 UTC
    Look at Damian Conway's Text::Autoformat It probably does what you want, or Text::Wrapper

    Update The regexps work... the modules are a little more robust...

                    - Ant

Re: Splitting A Word?
by lhoward (Vicar) on Jul 06, 2001 at 23:00 UTC
Re: Splitting A Word?
by dvergin (Monsignor) on Jul 06, 2001 at 23:30 UTC
    Actually,   s/(\S{30})(\S)/$1 $2/g   flubbs and gives you 31 characters in the 2nd and following chunks of a really long 'word' unless you do: 1 while $msg =~ s/(\S{30})(\S)/$1 $2/;
    ...omitting the trailing '/g'.

    So this would be a nice place to use a zero-width positive look-ahead assertion: $msg =~ s/(\S{30})(?=\S)/$1 /g;

Re: Splitting A Word?
by tachyon (Chancellor) on Jul 06, 2001 at 23:50 UTC

    This will do the trick

    $input = " 1234567890123456789012345678901234567890123456789012345678901234567890 + 1234567890123456789012345678901234567890123456789012345678901234567890 + 1234567890123456789012345678901234567890123456789012345678901234567890 + 1234567890123456789012345678901234567890123456789012345678901234567890 + "; # do the magic with a snazzy regex $input =~ s[(\S+)] # grab the words into $1 { local $_ = $1; # assign word to $_ if (length $1>30) { # if word to long s/(.{30})/$1 /g; # add spaces every 30 char } $_; # substitute in $_ for word # it may or may not have changed }gex; # global, execute, comments print $input;

    Note I have assumed that the words are any consecutive non whitespace chars




Re: Splitting A Word?
by damian1301 (Curate) on Jul 06, 2001 at 23:07 UTC
    You could just set the table width to a certain amount and that way the table will never change and it will wrap the text automatically...

    $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.

      no, usually the browser will expand the table to fit the text in it if there are really long words. that's the problem mpegman seems to be having. eg,

      <table width="2" border="1"> <tr><td>looooooooooooooong</td></tr> </table>

      gives you:


      certainly not 2 pixels wide.

      if you're working on a more liquid layout, you could modify one of the regexps given to substitute in a <wbr /> tag instead of a space. that tells the browser that if it needs to wrap text, that's a good spot to do it in. but if it doesn't need to wrap the text, you won't end up with a space in the middle of the word.

      anders pearson

        Oh really? Well, that is because it won't break the word for you. If you explicity state it's length (and restrict the use of <nobr>) then it will wrap on words. Eg.
        <table width="2" border="2"> <tr><td>yeah I am dave and blah blah blah blah blha</td></tr></table>
        which produces.
        yeah I am dave and blah blah blah blah blha

        $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.
Re: Splitting A Word?
by Anonymous Monk on Jul 06, 2001 at 23:01 UTC
    s/(\w{30})/$1 /g;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://94550]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2019-10-21 03:44 GMT
Find Nodes?
    Voting Booth?