#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    60
    Rep Power
    2

    Does anyone know how to get the wordwrap() to count number of words on one line.


    Hi there.

    I'm trying to create user comment system that is submitted by form, stored and pulled up by PHP.
    And I'm trying to tell the wordwrap() function to wrap sentences when it's longer than 20 characters for output.
    But it seems like it's wrapping text wherever it's 20th characters even when it's on the middle of the next line
    so the output's alignment looks messy. Some sentences are cut and wrapped in the middle of the sentence.
    I want the function to wrap the text around only when it exceeds over 20 characters. Not wherever it gets 20.
    Does this make sense?





    For example


    ---Original user's comment----------------------------------

    Some loooooooooooong text.
    Some loooooooooong text on the second line.
    Some looooooooooooooooong text on the third line.
    ------------------------------------------------------------

    //And the comment above will be sent to php script below

    PHP Code:
    if($_POST['submit'])
    {
     
    $someLongText $_POST['comment'];
     
    $output wordwrap($someLongText20"<br/>"true);          // Telling the text will get wrapped around at 20th character.
     
    $output str_replace("\r\n","<br/>\r\n",$output);                  // to preserve line breaks. 




    ---- In the file for output-----------------------------------
    <body>
    <?php $output ?>
    </body>

    -------------------------------------------------------------








    -----------------What you see on a browser------------------------

    Some loooooooooooong <---- takes up 20 character spaces
    text. <------only takes up 5 character spaces
    Some loooooooo <------ this line takes up 15 character spaces. 5+15=20. and it gets cut off in the middle of the sentence..
    oong text on the sec
    ond line.
    Some looooo
    oooooooooooong text
    on the third line.

    -----------------------------------------------------------------

    The wordwrap() doesn't care if it finds the 20th letter in the current line or the line below and once it finds the one, it just cut the word off in the middle.)


    How can I make the function to wrap up the text only when the number of characters exceed 20 on one line, please?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    wordwrap() will restart lines at the break string you provide. When you say "<br/>" then it will only break lines, and consider lines broken, at literally "<br/>".

    Instead use regular newlines and nl2br().
    PHP Code:
    $someLongText $_POST['comment'];
    $output wordwrap($someLongText20"\r\n"true);
    $output nl2br($output); 
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    60
    Rep Power
    2
    Thank you for your reply

    Does wordwrap() restart lines at <br/>??
    that doesn't make sense to me because it seems like the wordwrap() doesn't care the <br/> tag and that's why the lines after line breaks broken by <br/> tag is included in counting process, right??

    That being said, your code seems fine.
    I will try that out.

    Thank you.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    wordwrap() will restart lines at whatever break string you provide, so in your case yes it would restart on "<br/>"s.

    You do know that POST content has newlines \r\n and not HTML <br/>s, right?
    PHP Code:
    $_POST['comment'] = "Some loooooooooooong text.\r\nSome loooooooooong text on the second line.\r\nSome looooooooooooooooong text on the third line."

IMN logo majestic logo threadwatch logo seochat tools logo