Jump to content

substr()


dreamwest

Recommended Posts

Here is a function I found with a search

function WordLimiter($text,$limit=20){
    $explode = explode(' ',$text);
    $string  = '';

    $dots = '...';
    if(count($explode) <= $limit){
        $dots = '';
    }
    for($i=0;$i<$limit;$i++){
        $string .= $explode[$i]." ";
    }
    if ($dots) {
        $string = substr($string, 0, strlen($string));
    }

    return $string.$dots;
}


// Usage
$str = 'This is a long string. Could be longer too.';

echo WordLimiter($str,4);

Link to comment
https://forums.phpfreaks.com/topic/229555-substr/#findComment-1182693
Share on other sites

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.