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

Hi Monks

I have a VERY long string with no spaces or line breaks of any kind and I want to display this on a webpage, however I don't want it to scroll across the screen and make the user scroll sideways to see it. I want to force it to wrap onto the next line when it gets to the edge of the page, even though there are no possible line breaks. How can I do this?

String looks a bit like this

sdfsdfasdfaskjhasdkhdglkjhsdgkljhdgfkjhdasfjhkljsdahgklsjgkajshgfkaljdghdalkhj etc

Thanks
Tom

  • Comment on Forcing a string with no spaces to wrap to fit on the screen

Replies are listed 'Best First'.
Re: Forcing a string with no spaces to wrap to fit on the screen
by pbeckingham (Parson) on Jun 09, 2004 at 15:59 UTC

    I believe this is answered on this site, but consider just replacing lengthy chunks with that chunk plus a newline character. Or <br /> tags if this is html.

    $string =~ s/([^\n]{80})/$1\n/mg;

Re: Forcing a string with no spaces to wrap to fit on the screen
by ChrisS (Monk) on Jun 09, 2004 at 16:12 UTC
    You could use the Text::Wrap module. Straight from the docs:
    use Text::Wrap; $Text::Wrap::columns=10; print wrap "", "", "This is a long string and should be wrapped."; # prints # This is a # long # string # and # should be # wrapped.
    This code will also work if there are no spaces in your text.
•Re: Forcing a string with no spaces to wrap to fit on the screen
by merlyn (Sage) on Jun 09, 2004 at 16:09 UTC
    HTML 4.0 includes the &shy; entity which permits an optional line break. Of course, this requires that the browser implement standard HTML. Good luck. Here's a sample paragraph using that (see the source), and if it works in your browser, life is good!

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

    Edit by tye, put long line in READMORE

      Thanks for the suggestions, i've tried each and with varying success. I like the &shy;. The main problem with that is that i'm using Mason, and Mason has a handy |h thing which strips out HTML and things like &anything before displaying the string to the screen, so I can't really make any substitutions and add HTML code in... even escaped HTML code. So i'm a little bit stuck. I can of course just not use the Mason |h flag, but ideally I would like to just put everything in a table cell and FORCE that cell not to be more than x pixels wide. However if there are no line breaks or spaces in the string I don't think there is a HTML construct that will allow me to do this.

      I hope I am making sense here.

      I suppose i'm looking for a HTML solution to wrap around the string to force it to fit into the space I want, rather than actually changing the string itself.

      Is this possible?

      Thanks,

      Tom

        Tom, maybe I'm missing something important, but HTML table cells will wrap automatically. Setting width=50% for a cell should force wrapping to a maximum of half the current total column span.

        I just tried it and I see the problem about spaces. Hmm. Afaik, somehow spaces are gonna have to go in there somehow. ..
        #!/usr/bin/perl my $bigstring = 'qawsedrftgyhujikolplokijuhygtfrdeswaqawsedrftgyhujiko +lp'; my $width = 10; print padit($bigstring,$width,"\n"); sub padit { my $padme = shift; my $width = shift; my $separator = shift; my $position = 0; my $padded; while ($position < length $padme) { $padded .= substr $padme, $position, 1; $position++; if ($position % $width == 0) { $padded .= $separator; } } return $padded; }
        best of luck
        Andy
Re: Forcing a string with no spaces to wrap to fit on the screen
by iburrell (Chaplain) on Jun 09, 2004 at 16:08 UTC
    Define "edge of the page" for a web page where the user can resize the window to any size.

    The solution is to pick a length where you want to break the string. You can insert spaces to allow the browser to do the wrapping. Unluckily, the spaces will be visible if the browser doesn't wrap right there. Or you can insert <br> tags to force a break.

    One experiment you might try is using a zero-width space character (&#8203;) and see if browsers support it correctly. It should be considered whitespace and cause word wrap, but not show when in the middle of a line. Try inserting one every 20 characters or so.

Re: Forcing a string with no spaces to wrap to fit on the screen
by Zaxo (Archbishop) on Jun 10, 2004 at 03:49 UTC

    If you find a way to decide what line length you want, here's a simple way to wrap it:

    $ perl -e'my ($length, $string) = (42,"spdf" x 50); print substr($stri +ng, 0, $length, ""), $/ while $string;' spdfspdfspdfspdfspdfspdfspdfspdfspdfspdfsp dfspdfspdfspdfspdfspdfspdfspdfspdfspdfspdf spdfspdfspdfspdfspdfspdfspdfspdfspdfspdfsp dfspdfspdfspdfspdfspdfspdfspdfspdfspdfspdf spdfspdfspdfspdfspdfspdfspdfspdf $
    For HTML, of course, you would replace $/ with '<br />'.

    After Compline,
    Zaxo

Re: Forcing a string with no spaces to wrap to fit on the screen
by Anonymous Monk on Jun 10, 2004 at 09:02 UTC

    Ok thanks for the comments, I feel i'm getting close to a solution now... (I hope anyway)

    I have this code

    my $subject = "sdfsdfasdfaskjhasdkhdglkjhsdgkljhdgfkjhdasfjhkljsdahgkl +sjgkajshgfkaljdghdalkhj etc"; my $maxsize=125; push(@parts, substr($subject, 0, $maxsize, '')) while length $subject;

    This will break the string up into an array in sizes of $maxsize characters, however what I want to do is slightly cleverer. I want to split up the string based on spaces, then if it gets to greater than the maxsize without a space, then split there.

    So split on spaces, unless the part gets to be more than 125 characters, in which case split anyway.

    Thanks,
    Tom