this will "columnize" your long text. long text, especially on large
screens is really hard to read. thats why in the "real world" newspapers
use columns to layout text. with this script you just paste the
text and specify the numbers of columns. it will split the text into more
reader-friendly chunks.
“string to columns ” makes use of the built-in PHP functions
ceil( ) ,
strlen( ) ,
stripcslashes( ) ,
substr( ) ,
strpos( ) and
strtolower( ) .
01 <?php
02
03 $marginpercent = 3 ;
04 $topmarginpx = 200 ;
05 $bgcolor = 'whitesmoke' ;
06 $paddingpx = 5 ;
07 $seperators = array ( ' ' => 'words' , '. ' => 'sentences' , '<p>' => 'paragraphs' ) ;
08 if ( $_POST [ 'string' ] )
09 {
10 if ( $_POST [ 'column' ] ) $columns = $_POST [ 'column' ] ;
11 else $columns = 3 ;
12 if ( $_POST [ 'seperator' ] ) $seperator = $_POST [ 'seperator' ] ;
13 else $seperator = ' ' ;
14 $widthpercent = ceil ( ( ( 100 - ( 2 * $marginpercent ) ) / $columns ) - 2 ) ;
15 $stringlen = strlen ( $_POST [ 'string' ] ) ;
16 $percolumn = ceil ( $stringlen / $columns ) ;
17 for ( $x = 0 ; $x < $columns ; $x ++ )
18 {
19 $leftpx = ( ( $x * ceil ( ( 100 - ( 2 * $marginpercent ) ) / $columns ) ) + $marginpercent ) ;
20 $string = stripcslashes ( substr ( $_POST [ 'string' ] , ( $percolumn * $x ) , $percolumn ) ) ;
21 $nextstring = stripcslashes ( substr ( $_POST [ 'string' ] , ( $percolumn * ( $x + 1 ) ) , $percolumn ) ) ;
22 $nextpos = strpos ( strtolower ( $nextstring ) , strtolower ( $seperator ) ) ;
23 $nextword = substr ( $nextstring , 0 , ( $nextpos + strlen ( $seperator ) ) ) ;
24 $string = substr ( $string , $oldpos , $percolumn ) ;
25 $string = $string . $nextword ;
26 $oldpos = $nextpos + strlen ( $seperator ) ;
27 echo '
28 <div style="position:absolute; left:' . $leftpx . '%;
29 top:' . $topmarginpx . 'px; padding:' . $paddingpx . 'px;
30 width:' . $widthpercent . '%; background-color:' . $bgcolor . ';">
31 ' ;
32 if ( $x > 0 && $_POST [ 'heightpx' ] ) echo '
33 <div style="height:' . $_POST [ 'heightpx' ] . 'px; width:' . $widthpercent . '%;"> </div>' ;
34 echo $string ;
35 echo '
36 </div>
37 ' ;
38 }
39 } else {
40 echo '
41 <div style="position:absolute; left:' . $marginpercent . '%;
42 top:' . $topmarginpx . 'px; padding:' . $paddingpx . 'px;
43 width:' . ( 100 - ( 2 * $marginpercent ) ) . '%; background-color:' . $bgcolor . ';">
44 <form method="post" action="' . $_SERVER [ 'PHP_SELF' ] . '">
45 <textarea name="string" cols="70" rows="20"></textarea><br>
46 <input type="text" name="column" value="3" size="3"> columns | text split at
47 <input type="text" name="seperator" size="3">
48 <select name="preset" onchange="seperator.value=this.value">' ;
49 foreach ( $seperators as $k => $v )
50 {
51 echo '
52 <option value="' . $k . '">' . $v . '</option>' ;
53 }
54 echo '
55 </select> |
56 <input type="text" name="heightpx" value="" size="3"> topmargin on column 1+ |
57 <input type="submit" value="columnize">
58 </form>
59 </div>
60 ' ;
61 }
62
63 ?>