user968808
user968808

Reputation:

limit a string length by characters

Hi I have this piece of code and wanted to echo "</dd>\n", chr(20); with only 20 characters or whatever i choose. Where am i going wrong?

Thanks

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);
        }
    }
}

Upvotes: 0

Views: 167

Answers (1)

jprofitt
jprofitt

Reputation: 10964

chr() is used to display a character with the ASCII value given as the parameter. You're most likely looking to use substr() which will let you display part of the string (a substring).

$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);

should give you what you want.

Upvotes: 3

Related Questions