Posted By

takel on 02/23/11


Tagged

convert textcolumns


Versions (?)

Convert text to columns


 / Published in: PHP
 

  1. function text_convert2columns($colcontent, $columns)
  2. {
  3. $coloutput = '';
  4. $bodytext = array("$colcontent");
  5. $text = implode(',', $bodytext); //prepare bodytext
  6. $length = strlen($text); //determine the length of the text
  7. $length = ceil($length/$columns); //divide length by number of columns
  8. $words = explode(' ',$text); // prepare text for word count and split the body into columns
  9. $c = count($words);
  10. $l = 0;
  11.  
  12. for($i=1;$i<=$columns;$i++)
  13. {
  14. $new_string = '';
  15. $coloutput .= '<div class="txt-column-'.$i.'">';
  16. for($g=$l;$g<=$c;$g++)
  17. {
  18. if(strlen($new_string) <= $length || $i == $columns)
  19. {
  20. $new_string.=$words[$g]." ";
  21. }
  22. else
  23. {
  24. $l = $g;
  25. break;
  26. }
  27. }
  28.  
  29. $coloutput .= $new_string;
  30. $coloutput .= '</div>';
  31. }
  32. return $coloutput;
  33. }

Report this snippet  

You need to login to post a comment.