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