truncate

css

.ellipsis { 
    overflow: hidden; 
    white-space: nowrap; 
    text-overflow: ellipsis; 
}

php

$truncatedTitle = strlen($title) > 20 ? substr($title,0,20)."..." : $title;

blade

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Blade::directive('truncate', function($expression){

            list($string, $length) = explode(',',str_replace(['(',')',' '], '', $expression));

            return "<?php echo e(strlen({$string}) > {$length} ? substr({$string},0,{$length}).'...' : {$string}); ?>";
        });
    }
}

@truncate($card->title, 20) 

原文地址:https://www.cnblogs.com/fenle/p/5589649.html