要求:
语言:php4
将一段字符串分为两列显示,每列每行为30个字符,
就象这样(举个列):
hi hi hi hi hi hi hi hi hi hi hi hi hi hi
hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi
hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi当判断到每行的末尾的时候,如果是一个单词的话不能让该单词分为两行显示,
也就是不能出现这种情况:
as you can see some wo
rds
words被分开显示在两行,这是不允许的~希望能得到答案~~,谢谢~~~
语言:php4
将一段字符串分为两列显示,每列每行为30个字符,
就象这样(举个列):
hi hi hi hi hi hi hi hi hi hi hi hi hi hi
hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi
hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi当判断到每行的末尾的时候,如果是一个单词的话不能让该单词分为两行显示,
也就是不能出现这种情况:
as you can see some wo
rds
words被分开显示在两行,这是不允许的~希望能得到答案~~,谢谢~~~
overflow: hidden; text-overflow:ellipsis">
<NOBR>就是比如有一行文字,很长,表格内一行显示不下.</NOBR>
</DIV>
楼上兄弟,请用php好吗?
===============Z啊。这是HTML啊。你用什么都可以啊
我想,只要把我上面的思路进行一下改进,然后把函数写出来,应该可以实现楼主的要求的。回头我试着写写~
贴个自己的:(有问题,望能得到修改,我已经改了很久了~)
<?php
@header("Content-Type: image/jpeg");
include_once('header.php');
define('CHAR_COUNT_EACH_ROW',30);
define('PAPER_MAX_HEIGHT',580);
function maketrim($str){
return preg_replace("/@[^A-Za-z0-9\.\,\'\"]@/","",trim(str_replace("\n","",str_replace("\r","",$str))));
}function Generate($news_arr){
$img_src="newspaper.jpg";
$im = imagecreatefromjpeg($img_src);
$white=imagecolorallocate($im,0,0,0);
//fonts for paper name ,date,headline or news.you can change them here,but make sure the font could be found.
$fonts=array( 'name' =>'font_1165496078.ttf',
'date' =>'font_1165496040.ttf',
'headline' =>'font_1165496078.ttf',
'news' =>'font_1165491575.ttf');
$color = imagecolorallocatealpha($im, 0,0,0,64); $position_newspaper_name_x=75;
$position_newspaper_name_y=80;
$position_date_x=16;
$position_date_y=95;
$position_headline_x=30;
$position_headline_y=130;
$position_news_left_x=30;
$position_news_left_y=150; $name_font_size=64;
$date_font_size=10;
$headline_font_size=20;
$news_font_size=10;
$news=stripslashes(maketrim($news_arr['news']));
$total_line=ceil(strlen($news)/CHAR_COUNT_EACH_ROW);
$comline=ceil($total_line/3);
//$news_view1=" ";
$news_view=substr($news,0,CHAR_COUNT_EACH_ROW) ;
if(strlen($news)<CHAR_COUNT_EACH_ROW||strlen($news)==CHAR_COUNT_EACH_ROW)
{
$news_view1=$news_view;
}
else{
$lline=0;
while(strlen($news)>0)
{
if($news_view{CHAR_COUNT_EACH_ROW-1}==" ")
{
if($lline >$comline)
{
if($lline>($comline*2+1))
{
$news_view3.=$news_view."\n";
}
else{
$news_view2 .=$news_view."\n";
}
}
else
{
$news_view1.=$news_view."\n";
}
$lline++;
$news=substr($news,CHAR_COUNT_EACH_ROW);
if(strlen($news)<CHAR_COUNT_EACH_ROW)
{
$news_view=substr($news,0);
}
else{
$news_view=substr($news,0,CHAR_COUNT_EACH_ROW);
}
}
else
{
if($news{CHAR_COUNT_EACH_ROW}!=" ")
{
$i=0;
$j=0;
while(isset($news_view{$i}))
{
if($news_view{$i}==" ")
{
$j=$i;
}
$i++;
}
// unset($i);
$news_view=substr($news,0,$j); if($lline >$comline)
{
if($lline>($comline*2+1))
{
$news_view3.=$news_view."\n";
}
else{
$news_view2 .=$news_view."\n";
}
}
else
{
$news_view1.=$news_view."\n";
}
$lline++;
$news=substr($news,$j+1);
if(strlen($news)<CHAR_COUNT_EACH_ROW)
{
$news_view=substr($news,0);
}
else{
$news_view=substr($news,0,CHAR_COUNT_EACH_ROW) ;
}
}
else
{
$countspace=0;
while($news_view{$countspace}==" ")
{
$countspace++;
}
$news_view=substr($news_view,$countspace);
if($lline >$comline)
{
if($lline>($comline*2+1))
{
$news_view3.=$news_view."\n";
}
else{
$news_view2 .=$news_view."\n";
}
}
else
{
$news_view1.=$news_view."\n";
}
$lline++;
$news=substr($news,CHAR_COUNT_EACH_ROW);
if(strlen($news)<CHAR_COUNT_EACH_ROW)
{
$news_view=substr($news,0);
}
else{
$news_view=substr($news,0,CHAR_COUNT_EACH_ROW) ;
}
}
}
}
}
/* $news_array1=explode("\n",$news_view1);
$count=count($news_array1);
for($i=0;$i<$count;$i++)
{
$eachlinelenth=strlen($news_array1[$i]);
if($news_array1[$i]{0}==" ")
{
$news_array1[$i]=substr( $news_array1[$i],1);
$eachlinelenth=strlen($news_array1[$i]);
if() } }
*/
//echo $news;//here you should not see any thing.. imagettftext($im, $name_font_size, 0, $position_newspaper_name_x, $position_newspaper_name_y,
$color, $fonts['name'], stripslashes($news_arr['name']));
imagettftext($im, $date_font_size, 0, $position_date_x, $position_date_y,
$color, $fonts['date'],stripslashes($news_arr['date']));
imagettftext($im, $headline_font_size, 0, $position_headline_x, $position_headline_y,
$color, $fonts['headline'], stripslashes($news_arr['headline']));
// imagettftext($im, $news_font_size, 0, $position_news_left_x, $position_news_left_y,
// $color, $fonts['news'], stripslashes($news_left));
// imagettftext($im, $news_font_size, 0, $position_news_right_x, $position_news_right_y,
// $color, $fonts['news'], stripslashes($news_right));
imagettftext($im, $news_font_size, 0, $position_news_left_x, $position_news_left_y,
$color, $fonts['news'],$news_view1 ); imagettftext($im, $news_font_size, 0, $position_news_left_x+170, $position_news_left_y,
$color, $fonts['news'],$news_view2 );
imagettftext($im, $news_font_size, 0, $position_news_left_x+330, $position_news_left_y,
$color, $fonts['news'],$news_view3);
//cut off the blank
$height=160+16*$total_line/2+8;
if($height>PAPER_MAX_HEIGHT){
$height=PAPER_MAX_HEIGHT;
}
$blank_src=imagecreatetruecolor(370,$height);
imagecopymergegray($blank_src,$im,0,0,0,0,370,$height,100);
return $blank_src;
imagedestroy($im);
} if($env['id']!=''){
$id=abs(intval($env['id']));
$DB->query("select * from news where id = $id ");
$DB->exec_query();
$row=$DB->fetch_row();
$news_arr=$row;
$im=Generate($news_arr);
imagejpeg($im);
imagedestroy($im);
}?>
还有什么地方不符合你的要求?
也就是要做成以上页面中,报纸中的文字的显示效果
楼主,我能理解你的意思,你要求不仅能够自动换行,还能平均分配一行的单词,国外报纸的确是这样做的。相比之下,我们中文报纸就方便多了。函数npWordwrap能解决你的问题,你可以试试:
<?php //error_reporting(E_ALL);
$img = imagecreatefromjpeg("newspaper.jpg");
$date = $_POST["txtDate"];
$title = $_POST["txtTitle"];
$text = $_POST["txtText"];
$dateFont = 2;
$titleFont = 5;
$textFont = 2;
imagestring($img, $dateFont, 30, 130, $date, 0);
imagestring($img, $titleFont, 30, 160, $title, 0);
$textArray = explode("`", npWordwrap($text, 25, "`"));
$x = 30;
$y = 190;
foreach ($textArray as $line){
imagestring($img, $textFont, $x, $y, $line, 0);
$y += 15;
if ($y>500){
$x = 200;
$y = 190;
}
}
header("Content-type: image/jpeg");
imagejpeg($img);
function npWordwrap($string, $long, $br){
$lineArray = explode("`" ,wordwrap($string, $long+1, "`"));
for ($i=0; $i<count($lineArray); $i++){
$wordArray = explode(" ", $lineArray[$i]);
$mod = ($long+1-strlen($lineArray[$i]))%(count($wordArray)-1);
$div = ($long+1-strlen($lineArray[$i])-$mod)/(count($wordArray)-1);
for ($j=1; $j<count($wordArray)-1; $j++){
$wordArray[$j] = str_repeat(" ", $div).$wordArray[$j];
}
$wordArray[count($wordArray)-1] = str_repeat(" ", $div+$mod).$wordArray[count($wordArray)-1];
$lineArray[$i] = implode(" ", $wordArray);
}
return implode($br, $lineArray);
}
?>
$lineArray = explode("`" ,wordwrap($string, $long+1, "`"));
for ($i=0; $i<count($lineArray); $i++){
$wordArray = explode(" ", $lineArray[$i]);
if (count($wordArray)!=1){ // only one word?
$div = ($long+1-strlen($lineArray[$i])-$mod)/(count($wordArray)-1);
$mod = ($long+1-strlen($lineArray[$i]))%(count($wordArray)-1);
}
else{
$div = 0;
$mod = 0;
}
for ($j=1; $j<count($wordArray)-1; $j++){
$wordArray[$j] = str_repeat(" ", $div).$wordArray[$j];
}
$wordArray[count($wordArray)-1] = str_repeat(" ", $div+$mod).$wordArray[count($wordArray)-1];
$lineArray[$i] = implode(" ", $wordArray);
}
return implode($br, $lineArray);
}
还有就是说一句,函数使用方法同wordwrap()一样。
我做了一个小网页,楼主可以直接登陆看看,是不是符合你的要求:http://myzeitung.my.funpic.de/
{
echo 图片数组[$i];
if($i%3==0)echo "<br>";
}
aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbb asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg
asdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfad
</textarea><div style="width:250px;word-wrap:break-word;background-color:yellow;">
aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbb asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg
asdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfad
</div><div style="width:200px;word-break:keep-all;word-wrap:break-word;border:solid black 1px;display:inline;">
aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbb asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg
asdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfad
一二三四五六七八九〇一二三四五六七八九〇一二三四五六七八九〇一二三四五六七八九〇一二三四五六七八九〇..........................................................................</div>
<div style="width:200px;word-wrap:break-word;border:solid black 1px;display:inline;">
aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbb asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg asdfadsgsdfg
asdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfadsgsdfgasdfad
一二三四五六七八九〇一二三四五六七八九〇一二三四五六七八九〇一二三四五六七八九〇一二三四五六七八九〇..........................................................................</div>
我这个例子里是15个字符截一次.<script language="JavaScript">
var str = "Love, so soft and warm beside me, If I were to give my heart, It would have to be to you.";
alert(str.replace(/.{1,15}(?=$|\b)/ig, "$&\n"));
</script>
※以下是引用36楼 0009(夏天以南) 在2007-07-03 14:54:58 的发言:
──────────────────────────────────────────
不懂PHP,我写一个JavaScript的,原理一样,你修改一下就可以移到PHP上了.
我这个例子里是15个字符截一次.
<script language="JavaScript">
var str = "Love, so soft and warm beside me, If I were to give my heart, It would have to be to you.";
alert(str.replace(/.{1,15}(?=$|\b)/ig, "$&\n"));
</script>
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━这个正则是不处理长度大于15的单词,即大于15的单词会撑开,如果你要让长度大于15的单词自动折行可以这样处理<script language="JavaScript">
var str = "Love, so soft and warm beside me, If I were to give my heart, It would have to be to you.ooooooooooooooooooooooooooooooooooooooooooo,itz a testing.haha!";
alert(str.replace(/.{1,15}(?=$|\b)|[a-z]{15}/ig, "$&\n"));
</script>
var str = "Love, so soft and warm beside me, If I were to give my heart, It would have to be to you.oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo..............................................oooooooooooooooo itz a testing.haha";
alert(str.replace(/.{1,30}(?=$|\b)|[^\b]{30}/ig, "$&\n"));
</script>
这东西交给浏览器就可以了(用 CSS)!!能知道用户用什么字体吗?用几号?
循环查找字符串长度31,61,91...这些位置。换行条件为charcnt >=30 && currentchar != '\s'如果不满足currentchar != '\s'这个条件就继续向下找,直到满足currentchar != '\s'条件为止。