php breakup long lines


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function breakUpLongLines( $text, $maxLength=80 )
  2. {
  3. $counter = 0;
  4. $newText = '';
  5. $array = array();
  6.  
  7. $textLength = strlen( $text );
  8.  
  9. for( $i = 0; $i <= $textLength; $i++ )
  10. {
  11. $array[] = substr( $text, $i, 1 );
  12. }
  13.  
  14. $textLength = count( $array );
  15.  
  16. for( $x = 0; $x < $textLength; $x++ )
  17. {
  18. if( preg_match( "/[[:space:]]/", $array[ $x ] ) )
  19. {
  20. $counter = 0;
  21. }
  22. else
  23. {
  24. $counter++;
  25. }
  26.  
  27. $newText .= $array[ $x ];
  28.  
  29. if( $counter >= $maxLength )
  30. {
  31. $newText .= ' ';
  32.  
  33. $counter = 0;
  34. }
  35. }
  36.  
  37. return $newText;
  38. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.