Return to Snippet

Revision: 41832
at February 23, 2011 20:51 by takel


Initial Code
function text_convert2columns($colcontent, $columns)
{
  $coloutput = '';
  $bodytext = array("$colcontent");
  $text = implode(',', $bodytext); //prepare bodytext
  $length = strlen($text); //determine the length of the text
  $length = ceil($length/$columns); //divide length by number of columns
  $words = explode(' ',$text); // prepare text for word count and split the body into columns
  $c = count($words);
  $l = 0;
  
  for($i=1;$i<=$columns;$i++) 
  {
    $new_string = '';
    $coloutput .= '<div class="txt-column-'.$i.'">';
    for($g=$l;$g<=$c;$g++)
    {
      if(strlen($new_string) <= $length || $i == $columns)
      {
        $new_string.=$words[$g]." ";
      }
      else 
      {
        $l = $g;
        break;
      }
    }
    
    $coloutput .= $new_string;
    $coloutput .= '</div>';
  }
  return $coloutput;
}

Initial URL


Initial Description


Initial Title
Convert text to columns

Initial Tags
convert

Initial Language
PHP