You could do this in SQL as the others shown already.
But, if you also want to show the full text if the user clicks on it, you also need to full text and it seems a waste to let the database send you the short and the full text. So you could grab the full text and write some code to show the short text and the full text when the user clicks on it.
$result = mysql_query('SELECT text FROM table');
$row = mysql_fetch_row($result);
echo '<div onclick="alert(\''.$row[0].'\');">'.substr($row[0], 0, 40).'</div>';
Ofcourse you could do something nicer when you click on it (instead of alert()
). Also you could do some PHP checking now to see if the original is shorter than 40 characters and handle situations like this.