Timber\TextHelper::trim_characters PHP Метод

trim_characters() публичный статический Метод

This function can be useful for excerpt of the post As opposed to wp_trim_words trims characters that makes text to take the same amount of space in each post for example
С версии: 1.2.0
Автор: @CROSP
public static trim_characters ( string $text, integer $num_chars = 60, string | null $more = null ) : string
$text string Text to trim.
$num_chars integer Number of characters. Default is 60.
$more string | null Optional. What to append if $text needs to be trimmed. Default '…'.
Результат string trimmed text.
    public static function trim_characters($text, $num_chars = 60, $more = null)
    {
        if ($more === null) {
            $more = __('…');
        }
        $text = wp_strip_all_tags($text);
        $text = mb_strimwidth($text, 0, $num_chars, $more);
        return $text;
    }

Usage Example

Пример #1
0
 function testTrimCharacters()
 {
     $text = "Sometimes you need to do such weird things like remove all comments from your project.";
     $trimmed = \Timber\TextHelper::trim_characters($text, 20);
     $this->assertEquals("Sometimes yo…", $trimmed);
 }