按字符限制字符串长度

时间:2011-11-11 14:19:45

标签: php

嗨,我有这段代码,希望echo "</dd>\n", chr(20);只有20个字符或我选择的任何内容。 我哪里错了?

由于

class newsStory{
    var $title="";
    var $link="";
    var $description="";
    var $pubdate="";

    function show(){
        if($this->title){
            if($this->link){
                echo "<dt><a href=\"$this->link\">$this->title</a></dt>\n";
            }elseif($this->title){
                echo "<dt>$this->title</a></dt>\n";
            }
            echo "<dd>";
            if($this->pubdate)
                echo "<i>$this->pubdate</i> - ";
            if($this->description)
                echo "$this->description";
            echo "</dd>\n", chr(20);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

chr()用于显示以ASCII值作为参数的字符。您很可能希望使用substr()来显示字符串的一部分(子字符串)。

$characterLimit = 20;
$the_string = "Whatever string it is you're wanting to display in a shortened version.";
echo "</dd>\n", substr($the_string, 0, $characterLimit);

应该给你你想要的东西。