Limit string chars - PHP
Question
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 something 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!
OTHER TIPS
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 );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow