Jump to content

PHP limit characters in string


andretanguy

Recommended Posts

Hey there!

 

ok so at the moment i do this: <?php echo $row['short_desc']; ?>...

but i want to limit the output of short_desc to 300 chars.

 

i came across this:

 

<?php function truncate_string($string, $max_length){

    if (strlen($string) > $max_length) {

        $string = substr($string,0,$max_length);

        $string .= '..';

    }

return $string;

}?>

 

i need some help turning $string into my short_desc - if someone can help that'd be awesome

 

cheers

Andre

Link to comment
https://forums.phpfreaks.com/topic/42335-php-limit-characters-in-string/
Share on other sites

function trunc($details,$max) {

if(strlen($details)>$max) {

        $details = substr($details,0,$max);

        $i = strrpos($details," ");

        $details = substr($details,0,$i);

        $details = $details."...";

    }

    return $details;

}

 

echo trunc($row['short_desc']);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.