Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Word Wrap

  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post

    Word Wrap

    I have a simply comment system. I gave it a test with the regular idiotic aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa comment that we see every now and then, and as usual it messes up the layout, so I need some type of break in there. I decided to use the wordwrap, because last time I checked it worked. Not in my case though . I want a space after each 20th character. Why isn't this working?

    PHP Code:
    $komenti=$comments[$i]["comment"];
    $komenti=wordwrap($komenti20" ");
    echo(
    "$komenti"); 

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    wordwrap() doesn't wrap long words, it just splits sentences up just like the HTML should do for you.

    An example of word wrap:
    PHP Code:
    <?php
    $text 
    "The quick brown fox jumped over the lazy dog.";
    $newtext wordwrap($text20"<br />\n");

    echo 
    $newtext;
    ?> 

    Outputs:
    The quick brown fox<br />
    jumped over the lazy<br />
    dog.

    Try this:
    PHP Code:
    $komenti=$comments[$i]["comment"];
    $komenti=preg_replace('#\s(.{10,})(.*?)\s#''$1 $2'$komenti);
    echo(
    "$komenti"); 

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Actually wordwrap() will work fine for this, you just have to specify the fourth parameter as "true".

    string wordwrap ( string $str [, int $width [, string $break [, bool $cut]]] )

    If the cut is set to TRUE, the string is always wrapped at the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).

  • Users who have thanked Fumigator for this post:

    Erindesign (08-24-2007)

  • #4
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post
    Worked likea charm. Thanks Fumigator.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •