Resources for Developers Using Amazon Associates Web Services
· Home  
· Search  
· Browse Nodes  
· Data Feed?  
· One-Second Rule  
· PHP Examples  
· Tips for Associates  
· Tools  
· Understanding A2S  

Create a custom Amazon Associate Store in minutes with Associate-O-Matic.

Trimming Descriptions and Reviews

Often you would like to trim a product description or customer review to a shorter length. The following PHP function will trim a string to a desired length. It breaks the string at a space.

function cutString($text, $limit)
 {
  $text = strip_tags($text);
  if (strlen($text) > $limit)
    {
     $x = $limit;
     while ($x > ($limit * .8))
      {
       --$x;
       if (substr($text, $x, 1) == ' ') break;
      }
     $text = substr($text, 0, $x);
     return $text . "...";
    }
   else
    {
     return $text;
    }
 }

Copyright © 2009 by Roger Smith