BBS水木清华站∶精华区
发信人: thinkin (强强), 信区: Linux
标 题: Word Wrap
发信站: BBS 水木清华站 (Thu Feb 17 11:08:30 2000)
<?php
// text_wrap( $text, $max_width, $html_type )
// $text - Text to be formatted (a string)
// $max_width - Column on which to wrap
// $html_type - Should be "PRE" or "BLOCKQUOTE"
//
// Written by Jeffrey D. Webster of Mirada Innovations.
//
// This particular function was written for our clients, so that
// they could embed quotations from persons being interviewed
// without having to pay much attention to how the text was
// entered.
//
// Coming soon: Full justification function for <PRE> blocks.
//
// Version 1.0
Function text_wrap( $text, $max_width, $html_type )
{
// $cnt keeps track of the position of the last break
$cnt = 0;
if (!strcmp( "PRE", $html_type))
$line_break = "\n";
else
$line_break = "<BR>\n";
echo "<$html_type>\n";
for ($i = $max_width; $i < strlen($text); $i += $max_width)
{
$width = $max_width;
while ($text[$i] != ' ' && $text[$i] != '\n' && $text[i] != '\t')
{
$i--;
$width--;
}
echo substr($text, $cnt, $width),$line_break;
$cnt = $i+1;
}
$last_line = substr($text, $cnt);
if ($last_line[0] != ' ')
echo substr($text, $cnt);
else
echo substr($text, $cnt+1);
echo "</$html_type>\n";
}
// Do a little test.
// An arbitrary string ;)
$line = "\"When considering the use of a server side";
$line = "$line language, such as php3, you have to weigh";
$line = "$line the load on your server against the benefits";
$line = "$line of the language. For many, especially with";
$line = "$line regards to php3, the benefits are worth the";
$line = "$line sacrifice. For some tasks it is unimaginable";
$line = "$line how difficult they would be without server side";
$line = "$line scripting. There is certainly something to be";
$line = "$line said for web content that is dynamic -- it";
$line = "$line keeps people coming back.\" -- The Webmaster";
text_wrap($line, 60, "BLOCKQUOTE");
?>
--
人生到处知何似?
应似飞鸿踏雪泥。
泥上偶然留指爪,
鸿飞那复计东西!
※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.37.191]
BBS水木清华站∶精华区