PHP Classes

File: examples.php

Recommend this page to a friend!
  Classes of Tony   Text Fit   examples.php   Download  
File: examples.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Text Fit
Shorten text to fit within a limited length
Author: By
Last change: extra example for new version
Date: 11 years ago
Size: 6,211 bytes
 

Contents

Class file image Download
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
    <title>TextFit examples</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <style type="text/css">
    body { background:#eee; padding:10px; }
    h2 { font-size:18px; font-weight:normal; background:#fea; padding:2px 10px; float:left; clear:left; margin:5px 0;border:1px solid #ccc; }
    pre,.sample { background:#fff; padding:10px; float:left; clear:left; margin:0 0 20px 0; border:1px solid #ccc; }
    p.space { clear:left; margin:10px; }
    </style>
</head>
<body>

<?php

///////////////////////////////////////
/////
///// test page for TextFit.class.php
/////
///////////////////////////////////////



// TextFit class must be included..
require 'TextFit.class.php';



///////////////////////////////////////////////////////////////////////////////////////////////////
// Shorten examples..
// ----------------
echo '<h2>Shortening text:</h2><div class="sample">';
// shorten plain text..
echo TextFit::shortenText('Here is some quite long text string', 21)."<br />\r\n";
// ignore word boundaries..
echo TextFit::shortenText('Here is some quite long text string', 21, true)."<br />\r\n";
echo
"<br />\r\n";
// html tags will be removed, otherwise they may get broken..
$text = '<p>Any <b>HTML tags</b> in a string <a href="#">will be</a> removed before shortening.</p>';
echo
TextFit::shortenText($text, 45)."<br />\r\n";
echo
"<br />\r\n";
// shorten filename..
echo TextFit::shortenFilename('My special fancy new document.pdf', 20)."<br />\r\n";
echo
'</div>';



///////////////////////////////////////////////////////////////////////////////////////////////////
// Fit-text examples..
// -----------------
$ColumnDef = array(22, 15, 20, array('width'=>15, 'align'=>'right'));
$tf=new TextFit($ColumnDef);
echo
'<h2>Fit-text with shortening:</h2>'."\r\n";
echo
'<pre>'."\r\n";
echo
$tf->fitTextRow(array('Column 1','Column 2','Column 3','Column 4'));
echo
$tf->fitTextLine();
echo
$tf->fitTextRow(array( 'blah','blab','blat','14.75'));
echo
$tf->fitTextRow(array('1st column which is too long to fit','2nd column','3rd column which is also quite long','2,108.50'));
echo
$tf->fitTextRow(array('111111','222222','333333','4,444.44'));
echo
$tf->fitTextLine();
echo
"</pre>\r\n";

// with text wrapping..
echo '<h2>Fit-text with wrapping and blank lines:</h2>'."\r\n";
echo
'<pre>'."\r\n";
echo
$tf->fitTextRow(array('Column 1','Column 2','Column 3','Column 4'));
echo
$tf->fitTextLine('=');
$tf->setWrap(true, 0);
$tf->setAddRow(true);
echo
$tf->fitTextBlank();
echo
$tf->fitTextRow(array('blah','blab','blat','14.75'));
echo
$tf->fitTextRow(array('1st column which is longer than most', '2nd column',
   
'3rd column which is actually quite a bit longer than all the others, and will wrap over several lines',
   
'2,108.50'));
echo
$tf->fitTextRow(array('111111','222222','333333','4,444.44'));
echo
$tf->fitTextLine();
echo
"</pre>\r\n";



///////////////////////////////////////////////////////////////////////////////////////////////////
// Examples using multiple row array
// ---------------------------------
echo '<h2>From multi-row array:</h2>'."\r\n";
echo
'<pre>'."\r\n";
$Data=array(
        array(
           
'Name' => 'Nguyen Thi Anh',
           
'Country' => 'Vietnam',
           
'Username' => 'anh17'
           
),
        array(
           
'Name' => 'S. Baldrick',
           
'Country' => 'England',
           
'Username' => 'balders'
           
),
        array(
           
'Name' => 'Michael Bruce',
           
'Country' => 'Australia',
           
'Username' => '85commodore'
           
)
        );
$tf=new TextFit(17);
echo
$tf->fitTextRows($Data, true, true);
echo
'</pre>'."\r\n";

// simpler multi-row array..
$Data=array(
        array(
'Nguyen Thi Anh', 'Vietnam', 'anh17'),
        array(
'S. Baldrick', 'England', 'balders'),
        array(
'Michael Bruce', 'Australia', '85commodore')
        );
echo
'<pre>'."\r\n";
echo
$tf->fitTextRow(array('Who', 'Location', 'Login'));
echo
$tf->fitTextLine('~');
echo
$tf->fitTextRows($Data, false);
echo
'</pre>'."\r\n";

// with borders and separators..
$tf->setRowChar('|', '|');
$tf->setLineChar('+', '+');
echo
'<pre>'."\r\n";
echo
$tf->fitTextLine('-');
echo
$tf->fitTextRow(array('Who', 'Location', 'Login'));
echo
$tf->fitTextLine('-');
echo
$tf->fitTextRows($Data, false);
echo
$tf->fitTextLine('-');
echo
'</pre>'."\r\n";



///////////////////////////////////////////////////////////////////////////////////////////////////
// Example using a simple array (list)
// -----------------------------------
echo '<h2>Fit-text from a list:</h2>'."\r\n<pre>\r\n";

$Values=array('Cell 1', 'Cell 2', 'Cell 3', 'Cell 4', 'Cell 5', 'Number Six',
       
'SEVEN', '8', 'Number nine', '10', 'a lemon', '12th night', 'thirteen',
       
'fourteen', 'fifteen', 'sixteen', 'Seventeen');

$tf=new TextFit();
$tf->setPadChar(' ');
echo
$tf->fitTextList($Values, true, 3);
echo
"</pre>\r\n";



///////////////////////////////////////////////////////////////////////////////////////////////////
// Example using mysql database query result
// You should already have a database connection
// ---------------------------------------------

/*
    // Modify these 2 lines to suit your own query..
    $sql = 'select ClientId as Id, LastName, FirstName, EstabDate, ResSuburb, ResPostcode as PCode from client order by LastName, FirstName';
    $ColumnDef = array(6, 15, 15, array('width'=>12, 'align'=>'right'), 20, 6);

    echo '<h2>Database resultset:</h2>'."\r\n";
    echo '<pre>'."\r\n";
    $tf=new TextFit($ColumnDef);
    $tf->setRowChar('|');
    $result=mysql_query($sql);

    $row=array();
    while($row[]=mysql_fetch_assoc($result)) { } // get all rows

    echo $tf->fitTextRows($row, true);
    echo '</pre>'."\r\n";
*/




echo "<p class=\"space\">&nbsp;</p>\r\n";

?>

</body>
</html>