LemonInflux Posted October 7, 2007 Share Posted October 7, 2007 Say I call something from the database, like: <?php $sql = 'SELECT * FROM `blah`'; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)){ echo $row['lots_of_text']; } ?> My question is, is there a way for the code to display the first, say, 50 characters of lots_of_text, and then no more? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/72201-solved-show-x-characters/ Share on other sites More sharing options...
pocobueno1388 Posted October 7, 2007 Share Posted October 7, 2007 Give this a try echo substr($row['lots_of_text'], 0, 50); Link to comment https://forums.phpfreaks.com/topic/72201-solved-show-x-characters/#findComment-364090 Share on other sites More sharing options...
AV1611 Posted October 7, 2007 Share Posted October 7, 2007 Sure. if (strlen($row['lots_of_text']) <= 50) { echo $row['lots_of_text'];} else{ $str = $row['lots_of_text']; $str = substr($str,0,47); $str .= '...';} Now it will cut it off at 47 chracters and add ... to the end so they know the string is incomplete. You can even make the ... a hyperlink or put a balloon popup or whatever if they need to see the rest of the string. Link to comment https://forums.phpfreaks.com/topic/72201-solved-show-x-characters/#findComment-364093 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.