Page 1 of 1

Limiting characters

Posted: Mon Mar 13, 2006 11:16 am
by psurrena
Question: Say I have a news article stored in my database and I want to just list the title and a brief exerpt from in on say an index page.
Can I create an additional variable based on $body (below) that says to just return the first 100 characters or the article?

Thanks

Code: Select all

$query   = "SELECT title, body FROM news WHERE id=".$_GET['id']; 
    $result  = mysql_query($query) or die('Error : ' . mysql_error());  
    $row     = mysql_fetch_array($result, MYSQL_ASSOC);  
     
    $title   = $row['title']; 
    $body   = $row['body']; 
    $content .= "<hr><a href=\"news.php\">< Back</a><hr>
    			<p><strong>$title</strong></p>
    			<p>$body</p><hr>
    			<p><a href=\"news.php\">< Back</a> - <a href=\"#top\">Top</a></p><hr>";

Posted: Mon Mar 13, 2006 11:49 am
by ddragas

Code: Select all

echo nl2br(substr($body, 0, 100)) .  " ... " ;

Posted: Mon Mar 13, 2006 2:42 pm
by John Cartwright
Reading the Useful Post thread helps ;)

Which brings you how to properly chop text. Atleast this way you won't chop text in the middle of words, could even modify it to not chop in the middle of sentences I guess too.