#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,095
    Rep Power
    220

    Break the string if longer than 20 characters without space


    Hi I wanna make sure if they add something like !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I break it down.

    My code does nothing actually. As if I am not using the wordwrap.

    PHP Code:
    $comment wordwrap($row['comment_content'], 20" ");
         echo 
    html_escape($comment); 
    What am I doing wrong?

    Thanks
  2. #2
  3. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,177
    Rep Power
    9644
    So "break apart all words that are longer than 20 characters"?
    PHP Code:
    $new preg_replace('/\S{20}/''$0 '$old); 
    Or "do wordwrap but break a line in a word if it's too long"?
    PHP Code:
    wordwrap($row['comment_content'], 20" "true)
    //                                         ^ 
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,095
    Rep Power
    220
    Originally Posted by requinix
    So "break apart all words that are longer than 20 characters"?
    PHP Code:
    $new preg_replace('/\S{20}/''$0 '$old); 
    Or "do wordwrap but break a line in a word if it's too long"?
    PHP Code:
    wordwrap($row['comment_content'], 20" "true)
    //                                         ^ 

    Thanks

    I have been avoiding regex since 2003 (not something a Jedi says). Will this work?
    PHP Code:
    $string $row['comment_content'];
    $break 25;
    echo 
    implode(PHP_EOLstr_split(html_escape($string), $break)); 
  6. #4
  7. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,177
    Rep Power
    9644
    You want to break each line at exactly 25 characters? Even if it's in the middle of a word?

    Then yes, it'll work. But it'll also break in the middle of something that's been html_escaped()ed,
    Code:
    Then yes, it'll work.
     But it'll also break
     in the middle of somethi
    ng that's been html_e
    scaped()ed, so you should
     do that only at the end.
    so you should do that only at the end.
    PHP Code:
    $string $row['comment_content'];
    $break 25;
    echo 
    html_escape(implode(PHP_EOLstr_split($string$break))); 
    Code:
    Then yes, it'll work. But
     it'll also break in the 
    middle of something that'
    s been html_escaped()ed, 
    so you should do that onl
    y at the end.
    Last edited by requinix; July 8th, 2017 at 10:42 AM.
  8. #5
  9. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,177
    Rep Power
    9644
    Oh. Note that's the source view of the output. The HTML view of the before and after versions would be
    Code:
    Then yes, it'll work. But it'll also break in the middle of somethi ng that's been html_e scaped()ed, so you should do that only at the end.
    Code:
    Then yes, it'll work. But it'll also break in the middle of something that' s been html_escaped()ed, so you should do that onl y at the end.
    Doesn't look right, does it?
  10. #6
  11. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,095
    Rep Power
    220
    Your solution is what I wanted. Mine sucked.

IMN logo majestic logo threadwatch logo seochat tools logo