Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用字符限制字符串长度_Php - Fatal编程技术网

Php 用字符限制字符串长度

Php 用字符限制字符串长度,php,Php,嗨,我有这段代码,想echo“\n”,chr(20)仅包含20个字符或我选择的任何字符。 我哪里做错了 谢谢 class newsStory{ var $title=""; var $link=""; var $description=""; var $pubdate=""; function show(){ if($this->title){ if($this->link){

嗨,我有这段代码,想
echo“\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);
        }
    }
}
类新闻故事{
var$title=“”;
var$link=“”;
var$description=“”;
var$pubdate=“”;
函数show(){
如果($this->title){
如果($this->link){
回音“\n”;
}elseif($this->title){
回显“$this->title\n”;
}
回声“;
如果($this->pubdate)
回显“$this->pubdate-”;
如果($this->description)
回显“$this->description”;
回声“\n”,chr(20);
}
}
}
chr()
用于显示参数为ASCII值的字符。您很可能希望使用
substr()
,这将允许您显示部分字符串(子字符串)

$characterLimit=20;
$the_string=“您希望以缩短版本显示的任何字符串。”;
回显“\n”,substr(_字符串,0,$characterLimit);
应该提供您想要的。

chr()
用于显示参数为ASCII值的字符。您很可能希望使用
substr()
,这将允许您显示部分字符串(子字符串)

$characterLimit=20;
$the_string=“您希望以缩短版本显示的任何字符串。”;
回显“\n”,substr(_字符串,0,$characterLimit);

应该给你想要的。

怎么了?什么不起作用?您应该将所有
$this->property
放在
{}
中,并用双引号括起来:
echo“{$this->pubdate}-”出了什么问题?什么不起作用?您应该将所有
$this->property
放在
{}
中,并用双引号括起来:
echo“{$this->pubdate}-”
$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);