Echo first few words of a string

[font=Verdana, Arial, Helvetica][size=2][color=midnightblue]I’m really new to php and mysql so please forgive my ignorance. The only way I can do this stuff right now is with Dreamweaver MX and recordsets.

I have a recordset that displays an article summary from my database and I only want to display the first few words followed by …

This is what I have now and it retreives the whole summary.

<?php echo $row_featured['summary']; ?>

I figured out how to trim the output to a number of characters using substr like below, but it cuts off words.

<?php echo substr($row_featured['summary'],0,50); ?> 

Can anyone please help me change this code so it display the first 10 or 12 words followed by … ? If it’s easier to do it with mysql query I’m certainly open to that as well.

Thanks very much,


There are several functions people have written to do exactly this in the comments of the substr() function’s manual page at

this is not the all but this is how i got it some days before
it is base i feel

while ( $row = mysql_fetch_array( $result ) ) {

<tr bgcolor=“skyblue”><td>‘.substr($des, 0, 75).’…<a href=“view_carpet.php/?cid=‘. $row[‘carpet_id’].’”/><b>Details</b></a></td></tr>

it gives firsr 75 char of the data stored
(taken from php documentation)

Thanks for the quick reply. I have looked at that, but I’m afraid I just can’t figure out how to incorporate the fuctions into my existing code.

The example below seems the most appropriate, but can you please give me some advice for turning this:

   <?php echo $row_featured['summary']; ?>

into this:

   Truncates a string at a certain position without &#65533;cutting&#65533; words into senseless pieces.
   I hope this is usefull for some of you and that it is not another redundant note (I didn't find one) ...
    $string		= "Truncates a string at a certain position without »cutting« words into senseless pieces.";
    $maxlength	= 75;
    $extension	= " ...";
    function truncate_string ($string, $maxlength, $extension) {
        // Set the replacement for the "string break" in the wordwrap function
        $cutmarker = "**cut_here**";
        // Checking if the given string is longer than $maxlength
        if (strlen($string) > $maxlength) {
    	    // Using wordwrap() to set the cutmarker
    	    // NOTE: wordwrap (PHP 4 >= 4.0.2, PHP 5)
    	    $string = wordwrap($string, $maxlength, $cutmarker);
    	    // Exploding the string at the cutmarker, set by wordwrap()
    	    $string = explode($cutmarker, $string);
    	    // Adding $extension to the first value of the array $string, returned by explode()
    	    $string = $string[0] . $extension;
        // returning $string
        return $string;
    // This Will output:
    // "Truncates a string at a certain position without &#65533;cutting&#65533; ..."
    echo truncate_string ($string, $maxlength, $extension);

Thanks again.

You’d just stuck the function somewhere (anywhere) in your PHP code and then use it:

<?php echo truncate_string($row_featured['summary'], 50, ' ...'); ?>

Thank you so much. For some reason I thought I had to modify that whole block of code above the echo statement as well.

Have a great day! :smiley: