yii\helpers\BaseStringHelper::truncate PHP Method

truncate() public static method

Truncates a string to the number of characters specified.
public static truncate ( string $string, integer $length, string $suffix = '...', string $encoding = null, boolean $asHtml = false ) : string
$string string The string to truncate.
$length integer How many characters from original string to include into truncated string.
$suffix string String to append to the end of truncated string.
$encoding string The charset to use, defaults to charset currently used by application.
$asHtml boolean Whether to treat the string being truncated as HTML and preserve proper HTML tags. This parameter is available since version 2.0.1.
return string the truncated string.
    public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
    {
        if ($asHtml) {
            return static::truncateHtml($string, $length, $suffix, $encoding ?: Yii::$app->charset);
        }
        if (mb_strlen($string, $encoding ?: Yii::$app->charset) > $length) {
            return rtrim(mb_substr($string, 0, $length, $encoding ?: Yii::$app->charset)) . $suffix;
        } else {
            return $string;
        }
    }

Usage Example

Beispiel #1
1
		<!-- Left col -->
		<div class="col-lg-6">
		  <!-- Custom tabs (Charts with tabs)-->
		  <div class="box box-info">
			<div class="box-header with-border">
			  <h3 class="box-title">Top URI Request</h3>
			</div><!-- /.box-header -->
			<div class="box-body">
			  <div class="row">
				<div class="col-md-11">
					<?php 
Pjax::begin();
?>
					<?php 
echo GridView::widget(['dataProvider' => $dataProviderApi, 'filterModel' => $searchModelApi, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['attribute' => 'uri_access', 'value' => function ($model) {
    return BaseStringHelper::truncate($model->uri_access, 45, ' ... ', null, true);
}], 'count']]);
?>
					<?php 
Pjax::end();
?>
				</div>
			  </div><!-- /.row -->
			</div><!-- /.box-body -->
		  </div><!-- /.box -->
		</div><!-- /.Left col -->
		<div class="col-lg-6">
		  <!-- Custom tabs (Charts with tabs)-->
		  <div class="box box-info">
			<div class="box-header with-border">
			  <h3 class="box-title">Used IP Address</h3>
All Usage Examples Of yii\helpers\BaseStringHelper::truncate