本文整理汇总了PHP中yii\helpers\StringHelper::truncateWords方法的典型用法代码示例。如果您正苦于以下问题:PHP StringHelper::truncateWords方法的具体用法?PHP StringHelper::truncateWords怎么用?PHP StringHelper::truncateWords使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\helpers\StringHelper
的用法示例。
在下文中一共展示了StringHelper::truncateWords方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testTruncateWords
public function testTruncateWords()
{
$this->assertEquals('это тестовая multibyte строка', StringHelper::truncateWords('это тестовая multibyte строка', 5));
$this->assertEquals('это тестовая multibyte...', StringHelper::truncateWords('это тестовая multibyte строка', 3));
$this->assertEquals('это тестовая multibyte!!!', StringHelper::truncateWords('это тестовая multibyte строка', 3, '!!!'));
$this->assertEquals('это строка с неожиданными...', StringHelper::truncateWords('это строка с неожиданными пробелами', 4));
}
示例2: actionFeed
public function actionFeed($id, $link)
{
$newsFeed = NewsFeed::findOne(['id' => $id, 'published' => 1]);
if (!$newsFeed) {
throw new NotFoundHttpException("Лента новостей не найдена!");
}
if ($newsFeed->link != $link) {
return $this->redirect(['/rss/' . $newsFeed->fullLink]);
}
$response = \Yii::$app->getResponse();
$headers = $response->getHeaders();
$headers->set('Content-Type', 'application/rss+xml; charset=utf-8');
if (true || $newsFeed->hasUpdated()) {
$feed = new RssView(['dataProvider' => new ActiveDataProvider(['query' => $newsFeed->getNews(), 'sort' => ['defaultOrder' => ['publishDate' => SORT_DESC]], 'pagination' => ['pageSize' => array_key_exists('feedLength', $newsFeed->params) ? $newsFeed->params['feedLength'] : 30]]), 'channel' => ['title' => function ($widget, \Zelenin\Feed $feed) use(&$newsFeed) {
$feed->addChannelTitle($newsFeed->name);
}, 'link' => Url::toRoute('/', true), 'description' => $newsFeed->description, 'language' => function ($widget, \Zelenin\Feed $feed) {
return \Yii::$app->language;
}, 'image' => function ($widget, \Zelenin\Feed $feed) use(&$newsFeed) {
$feed->addChannelImage('//k-z.com.ua/images/' . (array_key_exists('image', $newsFeed->params) ? $newsFeed->params['image'] : 'logo1.png'), '//k-z.com.ua', 88, 31, $newsFeed->description);
}], 'items' => ['title' => function ($model, $widget, \Zelenin\Feed $feed) {
return $model->title;
}, 'link' => function ($model, $widget, \Zelenin\Feed $feed) {
return Url::toRoute([$model->fullLink], true);
}, 'guid' => function ($model, $widget, \Zelenin\Feed $feed) {
return Url::toRoute([$model->fullLink], true);
}, 'description' => function ($model, $widget, \Zelenin\Feed $feed) use(&$newsFeed) {
$text = htmlspecialchars_decode($model->textPreview);
if (array_key_exists('hideImages', $newsFeed->params) && $newsFeed->params['hideImages'] == 1) {
$text = strip_tags($text, '<a></a><p></p>');
}
$text = StringHelper::truncateWords(preg_replace('/\\&(\\#|)(\\w+|\\D+)\\;/m', '', $text), 50);
return $text;
}, 'yandex:full-text' => function ($model, $widget, $feed) use(&$newsFeed) {
$text = htmlspecialchars_decode($model->textPreview . $model->text);
if (array_key_exists('hideImages', $newsFeed->params) && $newsFeed->params['hideImages'] == 1) {
$text = strip_tags($text, '<a></a><p></p>');
}
$text = preg_replace('/\\&(\\#|)(\\w+|\\D+)\\;/m', '', $text);
return $text;
}, 'author' => function ($model, $widget, \Zelenin\Feed $feed) {
return $model->author;
}, 'dc:creator' => function ($model, $widget, \Zelenin\Feed $feed) {
return $model->author;
}, 'pubDate' => function ($model, $widget, \Zelenin\Feed $feed) {
$date = new \DateTime();
$date->setTimestamp($model->publishDate);
return $date->format(DATE_RSS);
}], 'standards' => ['xmlns:dc' => 'http://purl.org/dc/elements/1.1/', 'xmlns:yandex' => 'http://news.yandex.ru', 'xmlns' => 'http://backend.userland.com/rss2']]);
$feedContent = $feed->run()->__toString();
\Yii::$app->cache->set('rssfeed-' . $newsFeed->id . '-content', $feedContent);
} else {
$feedContent = \Yii::$app->cache->get('rssfeed-' . $newsFeed->id . '-content');
}
echo $feedContent;
}
示例3: chooseQuote
protected function chooseQuote()
{
if (count($this->quotes) == 0) {
return;
}
$this->currentQuote = $this->quotes[array_rand($this->quotes)];
if (isset($_REQUEST['DEBUG'])) {
$this->currentQuote = $this->quotes[0];
}
$this->currentSnippet = StringHelper::truncateWords($this->currentQuote, 6);
}
示例4: actionRss
public function actionRss()
{
$dataProvider = new ActiveDataProvider(['query' => Article::find()->with(['user'])->orderby('id DESC'), 'pagination' => ['pageSize' => 10]]);
$response = \Yii::$app->getResponse();
\Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
header("Content-Type: application/xml; charset=UTF-8");
$response->content = \Zelenin\yii\extensions\Rss\RssView::widget(['dataProvider' => $dataProvider, 'channel' => ['title' => Yii::$app->name, 'link' => Url::toRoute('/', true), 'description' => 'Articles ', 'language' => Yii::$app->language], 'items' => ['title' => function ($model, $widget) {
return $model->title;
}, 'description' => function ($model, $widget) {
return StringHelper::truncateWords($model->content, 50);
}, 'link' => function ($model, $widget) {
return Url::toRoute(['article/view', 'id' => $model->id], true);
}, 'author' => function ($model, $widget) {
return $model->user->email . ' (' . $model->user->name . ')';
}, 'pubDate' => function ($model, $widget) {
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $model->date);
return $date->format(DATE_RSS);
}]]);
}
示例5: actionRss
public function actionRss($category_id = null)
{
return RssView::widget(['dataProvider' => new ActiveDataProvider(['query' => Post::find()->published()->category($category_id)->language(Yii::$app->language)->orderBy(['published_at' => SORT_DESC]), 'pagination' => ['pageSize' => $this->module->rssPageSize]]), 'channel' => ['title' => Yii::$app->siteName, 'link' => Url::toRoute(['', 'category_id' => $category_id], true), 'description' => $category_id ? $this->loadCategoryModel($category_id)->title : Yii::t('gromver.platform', 'All news'), 'language' => Yii::$app->language], 'items' => ['title' => function ($model) {
/** @var $model \gromver\platform\news\models\Post */
return $model->title;
}, 'description' => function ($model) {
/** @var $model \gromver\platform\news\models\Post */
return $model->preview_text ? $model->preview_text : StringHelper::truncateWords(strip_tags($model->detail_text), 40);
}, 'link' => function ($model) {
/** @var $model \gromver\platform\news\models\Post */
return Url::toRoute($model->getFrontendViewLink(), true);
}, 'author' => function ($model) {
/** @var $model \gromver\platform\news\models\Post */
return $model->user->email . ' (' . $model->user->username . ')';
}, 'guid' => function ($model) {
/** @var $model \gromver\platform\news\models\Post */
return Url::toRoute($model->getFrontendViewLink(), true) . ' ' . Yii::$app->formatter->asDatetime($model->updated_at, 'php:' . DATE_RSS);
}, 'pubDate' => function ($model) {
/** @var $model \gromver\platform\news\models\Post */
return Yii::$app->formatter->asDatetime($model->published_at, 'php:' . DATE_RSS);
}]]);
}
示例6: getBriefText
public function getBriefText($asHtml = true)
{
return StringHelper::truncateWords($this->text, 40, $suffix = '...', $asHtml);
}
示例7:
* File name: _item.php
* @var $model \app\modules\content\models\ContentArticles;
*/
?>
<div class="container">
<article class="main-news-article wow fadeIn" data-wow-offset="50" data-wow-duration="1.5s" data-wow-delay="0s">
<div class="main-news-article__image col-sm-4">
<img src="<?php
echo $model->getUploadUrl('image');
?>
" alt="<?php
echo $model->title;
?>
">
</div>
<div class="main-news-article__text col-sm-8">
<h4><?php
echo $model->title;
?>
</h4>
<p><?php
echo \yii\helpers\StringHelper::truncateWords(strip_tags($model->content), 20);
?>
</p>
<?php
echo \app\modules\admin\widgets\Html::a(Yii::t('text', 'Read more...'), ['/content/article/view', 'catslug' => $model->category->slug, 'slug' => $model->slug]);
?>
</div>
</article>
</div>
示例8:
echo $firstNews->title;
?>
" src="<?php
echo $firstNews->imagePreview;
?>
">
</div>
</a>
<div class="tleft fleft">
<a class="readon inline readMore" href="<?php
echo $firstNews->fullLink;
?>
">Подробнее</a>
<p class="nspText longText" lang="ru">
<?php
echo ($textWords = $theme->textWords) ? StringHelper::truncateWords($firstNews->getTextPreview(0, '<a></a>'), $textWords) : $firstNews->getTextPreview(0, '<a></a>');
?>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="nspLinksWrap right">
<div class="nspLinks">
<ul class="nspList">
<?php
foreach ($topNews as $news) {
?>
<li>
示例9:
?>
<div class="popular-post widget">
<h3>Популярные комментарии</h3>
<ul>
<?php
foreach ($comments as $comment) {
?>
<li>
<a href="<?php
echo $comment->entity . '#' . $comment->id;
?>
">
<?php
echo StringHelper::truncateWords($comment->content, 20);
?>
</a>
<br>
<time>
<i class="glyphicon glyphicon-time"></i> <?php
echo $comment->created_at;
?>
</time>
</li>
<?php
}
?>
</ul>
</div>
示例10: actionRss
/**
* Main RSS channel.
* @return string
*/
public function actionRss()
{
$response = Yii::$app->getResponse();
$headers = $response->getHeaders();
$headers->set('Content-Type', 'application/rss+xml; charset=utf-8');
$response->content = RssView::widget(['dataProvider' => (new Forum())->search(null, true), 'channel' => ['title' => Config::getInstance()->get('name'), 'link' => Url::to(['default/index'], true), 'description' => Config::getInstance()->get('meta_description'), 'language' => Yii::$app->language], 'items' => ['title' => function ($model, $widget) {
if (!empty($model->latest)) {
return Html::encode($model->latest->thread->name);
} else {
return Html::encode($model->name);
}
}, 'description' => function ($model, $widget) {
if (!empty($model->latest)) {
return StringHelper::truncateWords($model->latest->content, 50, '...', true);
}
return '';
}, 'link' => function ($model, $widget) {
if (!empty($model->latest)) {
return Url::to(['default/show', 'id' => $model->latest->id], true);
}
return Url::to(['default/forum', 'cid' => $model->category_id, 'id' => $model->id, 'slug' => $model->slug], true);
}, 'author' => function ($model, $widget) {
if (!empty($model->latest)) {
return $model->latest->author->username;
}
return 'Podium';
}, 'guid' => function ($model, $widget) {
if (!empty($model->latest)) {
return Url::to(['default/show', 'id' => $model->latest->id], true) . ' ' . Yii::$app->formatter->asDatetime($model->latest->updated_at, 'php:' . DATE_RSS);
} else {
return Url::to(['default/forum', 'cid' => $model->category_id, 'id' => $model->id, 'slug' => $model->slug], true) . ' ' . Yii::$app->formatter->asDatetime($model->updated_at, 'php:' . DATE_RSS);
}
}, 'pubDate' => function ($model, $widget) {
if (!empty($model->latest)) {
return Yii::$app->formatter->asDatetime($model->latest->updated_at, 'php:' . DATE_RSS);
} else {
return Yii::$app->formatter->asDatetime($model->updated_at, 'php:' . DATE_RSS);
}
}]]);
}
示例11:
?>
<?php
}
?>
</div>
<div class="col-sm-9 col-md-10 post">
<h1>
<?php
echo $isFull ? Html::encode($model->title) : Html::a(Html::encode($model->title), ['view', 'id' => $model->id]);
?>
</h1>
<div class="content">
<?php
$text = HtmlPurifier::process(Markdown::process($model->text));
echo $isFull ? $text : StringHelper::truncateWords($text, 70, '<p>' . Html::a('Read more', ['view', 'id' => $model->id]) . '</p>', true);
?>
<?php
if ($isFull) {
?>
<div class="meta">
<?php
if (!empty($model->link)) {
?>
<p><?php
echo Html::a(Html::encode($model->link), $model->link);
?>
</p>
<?php
}
示例12:
</tr>
</thead>
<tbody>
<?php
foreach ($posts as $post) {
?>
<tr>
<td><a href="<?php
echo Url::to(['default/show', 'id' => $post->id]);
?>
"><?php
echo Html::encode($post->thread->name);
?>
</a></td>
<td><span data-toggle="popover" data-container="body" data-placement="right" data-trigger="hover focus" data-html="true" data-content="<small><?php
echo str_replace('"', '"e;', StringHelper::truncateWords($post->content, 20, '...', true));
?>
</small>" title="<?php
echo Yii::t('podium/view', 'Post Preview');
?>
"><span class="glyphicon glyphicon-leaf"></span></span></td>
<td><a href="<?php
echo Url::to(['admin/view', 'id' => $post->author->id]);
?>
"><?php
echo $post->author->podiumName;
?>
</a></td>
<td><span data-toggle="tooltip" data-placement="top" title="<?php
echo Yii::$app->formatter->asDateTime($post->created_at, 'long');
?>
示例13: function
Carbon::setLocale(substr(Yii::$app->language, 0, 2));
// eg. en-US to en
?>
<div class="template-category-index">
<?php
echo GridView::widget(['id' => 'template-category-grid', 'dataProvider' => $dataProvider, 'resizableColumns' => false, 'pjax' => false, 'export' => false, 'responsive' => true, 'bordered' => false, 'striped' => true, 'panelTemplate' => '<div class="panel {type}">
{panelHeading}
{panelBefore}
{items}
<div style="text-align: center">{pager}</div>
</div>', 'panel' => ['type' => GridView::TYPE_INFO, 'heading' => Yii::t('app', 'Templates') . ' <small class="panel-subtitle hidden-xs">' . Yii::t('app', 'By Categories') . '</small>', 'footer' => false, 'before' => ActionBar::widget(['grid' => 'template-category-grid', 'templates' => ['{create}' => ['class' => 'col-xs-6 col-md-8'], '{bulk-actions}' => ['class' => 'col-xs-6 col-md-2 col-md-offset-2']], 'bulkActionsItems' => [Yii::t('app', 'General') => ['general-delete' => Yii::t('app', 'Delete')]], 'bulkActionsOptions' => ['options' => ['general-delete' => ['url' => Url::toRoute('delete-multiple'), 'data-confirm' => Yii::t('app', 'Are you sure you want to delete these template categories? All data related to each item will be deleted. This action cannot be undone.')]], 'class' => 'form-control'], 'elements' => ['create' => Html::a('<span class="glyphicon glyphicon-plus"></span> ' . Yii::t('app', 'Create Category'), ['create'], ['class' => 'btn btn-primary']) . ' ' . Html::a(Yii::t('app', 'Need to extend the app functionality?'), ['/addons'], ['data-toggle' => 'tooltip', 'data-placement' => 'top', 'title' => Yii::t('app', 'With our add-ons you can add great features and integrations to your forms. Try them now!'), 'class' => 'text hidden-xs hidden-sm'])], 'class' => 'form-control'])], 'toolbar' => false, 'columns' => [['class' => '\\kartik\\grid\\CheckboxColumn', 'headerOptions' => ['class' => 'kartik-sheet-style'], 'rowSelectedClass' => GridView::TYPE_WARNING], ['attribute' => 'name', 'format' => 'raw', 'value' => function ($model) {
return Html::a(Html::encode($model->name), ['templates/category', 'id' => $model->id]);
}], ['attribute' => 'description', 'value' => function ($model) {
if (isset($model->description)) {
return StringHelper::truncateWords(Html::encode($model->description), 15);
}
return null;
}], ['attribute' => 'updated_at', 'value' => function ($model) {
if (isset($model->updated_at)) {
return Carbon::createFromTimestampUTC($model->updated_at)->diffForHumans();
}
return null;
}, 'label' => Yii::t('app', 'Last updated')], ['class' => 'kartik\\grid\\ActionColumn', 'dropdown' => true, 'dropdownButton' => ['class' => 'btn btn-primary'], 'dropdownOptions' => ['class' => 'pull-right'], 'buttons' => ['view' => function ($url) {
$options = array_merge(['title' => Yii::t('app', 'View'), 'aria-label' => Yii::t('app', 'View'), 'data-pjax' => '0'], []);
return '<li>' . Html::a('<span class="glyphicon glyphicon-eye-open"></span> ' . Yii::t('app', 'View Record'), $url, $options) . '</li>';
}, 'update' => function ($url) {
$options = array_merge(['title' => Yii::t('app', 'Update'), 'aria-label' => Yii::t('app', 'Update'), 'data-pjax' => '0'], []);
return '<li>' . Html::a('<span class="glyphicon glyphicon-pencil"></span> ' . Yii::t('app', 'Update'), $url, $options) . '</li>';
}, 'delete' => function ($url) {
$options = array_merge(['title' => Yii::t('app', 'Delete'), 'aria-label' => Yii::t('app', 'Delete'), 'data-confirm' => Yii::t('app', 'Are you sure you want to delete this template category?
示例14:
<?php
}
foreach ($items as $item) {
?>
<li class="rss-post">
<h2>
<?php
echo Html::a($item->get_title(), $item->get_permalink(), ['target' => '_blank']);
?>
<?php
if (isset($options['wrapperTagForDate'])) {
?>
<?php
echo Html::tag($options['wrapperTagForDate'], $item->get_date('D, d M Y H:i:s'));
?>
<?php
} else {
?>
<?php
echo $item->get_date('D, d M Y H:i:s');
?>
<?php
}
?>
</h2>
<?php
echo StringHelper::truncateWords(strip_tags($item->get_content()), 40);
?>
</li>
<?php
}
示例15:
<img class="nspImage" alt="<?php
echo $latestNews->title;
?>
" src="<?php
echo $latestNews->imagePreview;
?>
">
</div>
<h4 class="nspHeader">
<?php
echo ($titleLen = ConfigurationHelper::getValue('i.titleWords')) ? StringHelper::truncateWords($latestNews->title, $titleLen) : $latestNews->title;
?>
</h4>
</a>
<?php
echo Html::tag('div', ($textLen = ConfigurationHelper::getValue('i.textWords')) ? StringHelper::truncateWords($latestNews->getTextPreview(0, ''), $textLen) : $latestNews->getTextPreview(0, ''), ['class' => 'nspText textPreview', 'lang' => 'ru']);
?>
</div>
</div>
</div>
<div id="gkToptop3" class="gkCol gkColRight">
<?php
echo \frontend\widgets\LastNewsWidget::widget();
?>
</div>
</div>
<div id="gkTop2" class="gkMain gkWrap">
<div class="box nsp grey dotted">
<div>
<h3 class="header">
<span>Новости дня</span>