Truncate string in Laravel blade templates

后端 未结 11 1066
死守一世寂寞
死守一世寂寞 2020-12-07 15:28

Is there a truncate modifier for the blade templates in Laravel, pretty much like Smarty?

I know I could just write out the actual php in the template but i\'m looki

相关标签:
11条回答
  • 2020-12-07 16:05

    In Laravel 4 & 5 (up to 5.7), you can use str_limit, which limits the number of characters in a string.

    While in Laravel 5.8 up, you can use the Str::limit helper.

    //For Laravel 4 to Laravel 5.5
    {{ str_limit($string, $limit = 150, $end = '...') }}
    //For Laravel 5.5 upwards
    {{ \Illuminate\Support\Str::limit($string, 150, $end='...') }}
    

    For more Laravel helper functions http://laravel.com/docs/helpers#strings

    0 讨论(0)
  • 2020-12-07 16:06

    You can set string limit as below example:

    <td>{{str_limit($biodata ->description, $limit = 20, $end = '...')}}</td>
    

    It will display only the 20 letters including whitespaces and ends with ....

    Example image

    0 讨论(0)
  • 2020-12-07 16:07

    Update for Laravel 7.*: Fluent Strings i.e a more fluent, object-oriented interface for working with string values, allowing you to chain multiple string operations together using a more readable syntax compared to traditional string operations.

    limit Example :

    $truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20);
    

    Output

    The quick brown fox...
    

    words Example :

    $string = Str::of('Perfectly balanced, as all things should be.')->words(3, ' >>>');
    

    Output

    Perfectly balanced, as >>>
    

    Update for Laravel 6.* : You require this package to work all laravel helpers composer require laravel/helpers

    For using helper in controller, don't forget to include/use class as well

    use Illuminate\Support\Str;
    

    Laravel 5.8 Update

    This is for handling characters from the string :

    {!! Str::limit('Lorem ipsum dolor', 10, ' ...') !!}
    

    Output

    Lorem ipsu ... 
    

    This is for handling words from the string :

    {!! Str::words('Lorem ipsum dolor', 2, ' ...') !!}
    

    Output

    Lorem ipsum ... 
    

    Here is the latest helper documentation for handling string Laravel Helpers

    0 讨论(0)
  • 2020-12-07 16:09

    For simple things like this I would prefer to make a helper - for example:

    create a helpers.php file in your /app/helpers.php with following content:

    <?php
    if (! function_exists('short_string')) {
        function short_string($str) {
                $rest = substr($str, 0, 10);
                return $rest;
        }
    }
    

    Register the helper.php at autoload in your composer.json

       "autoload": {
            "files": [
                "app/helpers.php"
            ],
            "psr-4": {
                "App\\": "app/"
            },
            "classmap": [
                "database/seeds",
                "database/factories"
            ]
        }
    

    After that you can use in your blade file for example:

    {{ short_string($whatever_as_text) }}
    

    You can use this simple function, then, globally in your app.

    0 讨论(0)
  • 2020-12-07 16:14

    In Laravel 4 & 5 (up to 5.7), you can use str_limit, which limits the number of characters in a string.

    While in Laravel 7 up, you can use the Str::limit helper.

    //For Laravel  to Laravel 7
    
    {{ Illuminate\Support\Str::limit($post->title, 20, $end='...') }}
    
    0 讨论(0)
提交回复
热议问题