开发者

Limit string chars - PHP

I am building an RSS script with mysql, and i dont want an extra field in the database... i want to shrink down the body of the article with a "... Read More" but im not sure how i can limit the number of chars echoed out onto the page?

Of course not this syntax, but somethi开发者_如何学Pythonng along the lines of:

echo(limit($row['newsBody'], 1000));

I dont mind if it takes 15 lines of code to do this ;)

P.S. I am sure limit() is a function, please dont tell me .. its just an example ;)

Thanks in advance!


echo(substr($row['newsBody'], 0, 1000));

The substr function is what you are looking for. Or you can use mb_substr if you are dealing with multi-byte strings.


You could do this

$body = $row['newsBody'];
$length = strlen($body) > 1000 ? 1000 : strlen($1000);
echo substr($body,0, $length);

It'll print the first 1000 characters or the whole message which ever is shorter


substr( $string, 0, 1000 );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜