在while循环中使用wordwrap

时间:2013-09-02 18:14:27

标签: php word-wrap

while($row = mysql_fetch_array($result))
{
echo $row['title']
}

您好,上面的代码在表格中输出主题的标题。但是,我希望标题在一定数量的字符后中断,并用三个点替换最后一个字符。

例如不是

blalblablalbalbalblalbalblalba

... blalblblalbalbla

我想我必须像这样用wordwrap做这个......

$title= $row['title'];
$newtitle = wordwrap($title, xx, "...", true);

但是,我不知道如何把它放在while循环中。

1 个答案:

答案 0 :(得分:0)

听起来你不想要wordwrap,而是想要子字符串

while($row = mysql_fetch_array($result)){
    //1) check if string lenth is too long (e.g. 10 characters)
    if(strlen($row['title'])>10){
        //2a) Truncate the string and append the ...
        echo substr($row['title'],0,10)." ...";
    }else echo the the string as it's not too long
    //2b)
    echo $row['title']
}

您可能还需要修改此代码以包含定义表格的字符...