yupe\helpers\YText::wordLimiter PHP Method

wordLimiter() public static method

Обрезать текст до определенного колиства слов, добавив в конце "..."
public static wordLimiter ( $str, $limit = 100, $end_char = '…' ) : string
return string - новая строка
    public static function wordLimiter($str, $limit = 100, $end_char = '…')
    {
        if (trim($str) == '') {
            return $str;
        }
        preg_match('/^\\s*+(?:\\S++\\s*+){1,' . (int) $limit . '}/', $str, $matches);
        if (mb_strlen($str) == mb_strlen($matches[0])) {
            $end_char = '';
        }
        return rtrim($matches[0]) . $end_char;
    }

Usage Example

Example #1
0
    <h1>
        <?php 
echo Yii::t('StoreModule.store', 'Products');
?>
        <small><?php 
echo Yii::t('StoreModule.store', 'administration');
?>
</small>
    </h1>
</div>

<?php 
$this->widget('yupe\\widgets\\CustomGridView', ['id' => 'product-grid', 'sortableRows' => true, 'sortableAjaxSave' => true, 'sortableAttribute' => 'position', 'sortableAction' => '/store/productBackend/sortable', 'type' => 'condensed', 'dataProvider' => $model->search(), 'filter' => $model, 'actionsButtons' => ['add' => CHtml::link(Yii::t('StoreModule.store', 'Add'), ['/store/productBackend/create'], ['class' => 'btn btn-sm btn-success pull-right']), 'copy' => CHtml::link(Yii::t('StoreModule.store', 'Copy'), '#', ['id' => 'copy-products', 'class' => 'btn btn-sm btn-default pull-right', 'style' => 'margin-right: 4px;'])], 'columns' => [['type' => 'raw', 'value' => function ($data) {
    return CHtml::image(StoreImage::product($data, 40, 40), $data->name, ["width" => 40, "height" => 40, "class" => "img-thumbnail"]);
}], ['name' => 'name', 'type' => 'raw', 'value' => function ($data) {
    return CHtml::link(\yupe\helpers\YText::wordLimiter($data->name, 5), ["/store/productBackend/update", "id" => $data->id]);
}], ['class' => 'bootstrap.widgets.TbEditableColumn', 'name' => 'sku', 'editable' => ['emptytext' => '---', 'url' => $this->createUrl('/store/productBackend/inline'), 'mode' => 'inline', 'params' => [Yii::app()->getRequest()->csrfTokenName => Yii::app()->getRequest()->csrfToken]], 'filter' => CHtml::activeTextField($model, 'sku', ['class' => 'form-control'])], ['class' => 'yupe\\widgets\\EditableStatusColumn', 'name' => 'producer_id', 'url' => $this->createUrl('/store/productBackend/inline'), 'source' => CMap::mergeArray(['' => '---'], Producer::model()->getFormattedList()), 'editable' => ['emptytext' => '---']], ['name' => 'category_id', 'value' => function ($data) {
    $categoryList = '<span class="label label-primary">' . (isset($data->category) ? $data->category->name : '---') . '</span>';
    foreach ($data->categories as $category) {
        $categoryList .= '<br>' . $category->name;
    }
    return $categoryList;
}, 'type' => 'raw', 'filter' => CHtml::activeDropDownList($model, 'category_id', StoreCategoryHelper::formattedList(), ['encode' => false, 'empty' => '', 'class' => 'form-control']), 'htmlOptions' => ['width' => '220px']], ['class' => 'bootstrap.widgets.TbEditableColumn', 'name' => 'price', 'value' => function (Product $data) {
    return round($data->getBasePrice(), 2);
}, 'editable' => ['url' => $this->createUrl('/store/productBackend/inline'), 'mode' => 'inline', 'params' => [Yii::app()->getRequest()->csrfTokenName => Yii::app()->getRequest()->csrfToken]], 'filter' => CHtml::activeTextField($model, 'price', ['class' => 'form-control'])], ['class' => 'bootstrap.widgets.TbEditableColumn', 'name' => 'discount_price', 'header' => Yii::t('StoreModule.store', 'New price'), 'value' => function (Product $data) {
    return round($data->getDiscountPrice(), 2);
}, 'editable' => ['emptytext' => '---', 'url' => $this->createUrl('/store/productBackend/inline'), 'mode' => 'inline', 'params' => [Yii::app()->getRequest()->csrfTokenName => Yii::app()->getRequest()->csrfToken]], 'filter' => CHtml::activeTextField($model, 'discount_price', ['class' => 'form-control'])], ['class' => 'yupe\\widgets\\EditableStatusColumn', 'name' => 'in_stock', 'header' => Yii::t('StoreModule.store', 'Availability'), 'url' => $this->createUrl('/store/productBackend/inline'), 'source' => $model->getInStockList(), 'options' => [Product::STATUS_IN_STOCK => ['class' => 'label-success'], Product::STATUS_NOT_IN_STOCK => ['class' => 'label-danger']]], ['class' => 'bootstrap.widgets.TbEditableColumn', 'name' => 'quantity', 'value' => function (Product $data) {
    return $data->quantity;
}, 'header' => Yii::t('StoreModule.store', 'Rest'), 'editable' => ['url' => $this->createUrl('/store/productBackend/inline'), 'mode' => 'inline', 'params' => [Yii::app()->getRequest()->csrfTokenName => Yii::app()->getRequest()->csrfToken]], 'filter' => CHtml::activeTextField($model, 'price', ['class' => 'form-control'])], ['class' => 'yupe\\widgets\\EditableStatusColumn', 'name' => 'status', 'url' => $this->createUrl('/store/productBackend/inline'), 'source' => $model->getStatusList(), 'options' => [Product::STATUS_ACTIVE => ['class' => 'label-success'], Product::STATUS_NOT_ACTIVE => ['class' => 'label-info'], Product::STATUS_ZERO => ['class' => 'label-default']]], ['class' => 'yupe\\widgets\\CustomButtonColumn', 'buttons' => ['front_view' => ['visible' => function ($row, $data) {
    return $data->status == Product::STATUS_ACTIVE;
}]]]]]);
All Usage Examples Of yupe\helpers\YText::wordLimiter