I have a site that contains news articles that are added to a database.
I want to have a page that just gives a bit of a preview of the article such as maybe the first 50 words of the article, just so users can see if it is going to contain information they want to read or not.
Can anyone point me in the right direct/give and example of how to do this in PHP to display a specified number of words from a larger article in a MYSQL database. I'm hoping it is going to be a relatively simple bit of code to be honest...
I'm not renowned for my amazing php skills but i believe you're looking for is strlen() and then you can shorten the returned length to what ever value you like. The code i've been using in my projects is this...
Code:
<?php
function limit_text( $text, $limit )
{
// figure out the total length of the string
if( strlen($text)>$limit )
{
# cut the text
$text = substr( $text,0,$limit );
# lose any incomplete word at the end
$text = substr( $text,0,-(strlen(strrchr($text,' '))) );
}
// return the processed string
return $text;
}
?>
I like it as it deals with the words at the end of a sentence and doesn't leave your visitors hangi.....