How to cut the string without breaking any words

Dec 12, 2007 insic 8 Comments

The output is

function Cut($string, $max_length){
if (strlen($string) > $max_length){
$string = substr($string, 0, $max_length);
$pos = strrpos($string, ” “);
if($pos === false) {
return substr($string, 0, $max_length).”…”;
return substr($string, 0, $pos).”…”;
return $string;

$string = ‘How to cut the string without breaking any words’;
echo ‘

echo Cut($string,30);
echo ‘


About the author: insic

Subscribe in my RSS Feed for more updates on Web Design and Development related articles. Follow me on twitter or drop a message to my inbox.

  • Dennis

    Very use full code. Thanks RICH.

  • Pingback: site

  • ukay

    nice tut just a hint I think you don’t need the ellipsis(…) anymore for that, you can just put the read more text instead somewhat like how google adsense diplays their text in adsense

  • afsane

    thank u so much
    it work very good

  • Tolenca

    Muchas gracias…

  • alex

    Great function! Thanks!

  • John

    Great ! You saved my day !!

  • Holly

    Thank you so much for this!!.