Zend - The PHP Company




Text

Add Code


text2columns  

Type: code fragment
Added by: buffaloBuff
Entered: 30/01/2002
Last modified: 02/12/2001
Rating: - (fewer than 3 votes)
Views: 4692
Function created for columnizing larger textblocks faciliating layout. Returns either an array of n number of columns or, if return_formated is set to TRUE, a preformatted html table with n cells with css_class is returned. Hey... check the commented text for ref =).


<?php
/*
array text2columns(string text [,int n [, bool return_formated [, string css_class]]])

Function returns an array containing n number of entries (defaults to 2)
of 'equal' length (word wrap) from desired text. Created for columnizing
larger textblocks faciliating layout. Set return_formated flag to TRUE
if you'd like the columns returned within a prelayouted html table
(with="100%") (defaults to FALSE) and includes the css_class to all cells
(defaults to NULL).


*/

    
function text2columns($str$cols=2$formated=FALSE$class=NULL) {
        
$size strlen($str)/$cols;
        
$tmpstr explode(" "$str);
        
$i 0;
        for(
$j 0$j $cols$j++) {
            while(
$i <= sizeof($tmpstr)) {
                if(
strlen($col[$j]) < $size) {
                    
$col[$j] .= $tmpstr[$i]." ";
                    
$i++;
                }
                else break;
            }
            
rtrim($col[$j]);
        }
        if(
$formated != FALSE) {
            if(
$class != NULL)
                
$class ' class="'.$class.'"';
            
$form '<table width="100%" cellspacing="0"><tr'.$class.' valign="top">';
            for(
$i 0$i $cols$i++) {
                
$form .= '<td>'.$col[$i].'</td>';
            }
            
$form .= '</tr></table>';
            return 
$form;
        } else return 
$col;
    }

}

?>


Usage Example




Rate This Script





Search



This Category All Categories