本文整理汇总了PHP中yii\helpers\StringHelper::truncate方法的典型用法代码示例。如果您正苦于以下问题:PHP StringHelper::truncate方法的具体用法?PHP StringHelper::truncate怎么用?PHP StringHelper::truncate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\helpers\StringHelper
的用法示例。
在下文中一共展示了StringHelper::truncate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getMetaData
public function getMetaData()
{
$model = $this->getMetaModel();
$title = $model->title ?: $this->title;
$description = $model->description ?: StringHelper::truncate(strip_tags($this->content), 200);
return [$title, $description, $model->keywords];
}
示例2: formatConversation
/**
* @inheritDoc
*/
public function formatConversation($model)
{
$model = parent::formatConversation($model);
$model['date'] = DateHelper::formatConversationDate($model['created_at']);
$model['text'] = StringHelper::truncate($model['text'], 20);
return $model;
}
示例3: checkForNews
private function checkForNews()
{
try {
$response = (new Client())->createRequest()->setUrl($this->getCurrentFeed()->url)->send();
if (!$response->isOk) {
throw new \Exception();
}
$rss = simplexml_load_string($response->getContent());
$newItemsCount = 0;
foreach ($rss->channel->item as $item) {
if (!NewModel::findOne(['feed' => $this->getCurrentFeed()->id, 'url' => (string) $item->link])) {
$new = new NewModel();
$new->feed = $this->getCurrentFeed()->id;
$new->published_at = date_create_from_format(\DateTime::RSS, (string) $item->pubDate)->format('Y-m-d H:i:s');
$new->title = (string) $item->title;
if (isset($item->description)) {
$new->short_text = StringHelper::truncate(strip_tags((string) $item->description), 250);
}
$new->url = (string) $item->link;
if ($new->save()) {
$newItemsCount++;
}
}
}
if ($newItemsCount > 0) {
\Yii::$app->session->addFlash('info', \Yii::t('user', 'Get news: ') . $newItemsCount);
$this->clearOldNewsIfNeed();
\Yii::$app->cache->delete($this->getNewsCacheKey());
\Yii::$app->cache->delete($this->getFeedsCacheKey());
}
} catch (Exception $e) {
\Yii::$app->session->addFlash('danger', \Yii::t('user', 'Get news error'));
}
}
示例4: Preview
public static function Preview($models)
{
foreach ($models as $key => $model) {
$models[$key]['text'] = StringHelper::truncate(strip_tags($model['text']), 400);
}
return $models;
}
示例5: userInfo
/**
* @return array
* 构建登录栏数据
*/
private function userInfo()
{
if (Yii::$app->getUser()->isGuest) {
$userInfo = [['label' => '登录', 'url' => $user = Yii::$app->getUser()->loginUrl], ['label' => '注册', 'url' => Yii::$app->params['registerUrl']]];
} else {
$userInfo = [['label' => StringHelper::truncate(Yii::$app->user->identity->username, 5), 'active' => false, 'url' => ['/'], 'items' => [['label' => '个人资料', 'url' => '#'], ['label' => '退出', 'url' => ['/signout']]]]];
}
return $userInfo;
}
示例6: getFileName
static function getFileName($fileInstanse, $namePostfix = true)
{
$baseName = str_ireplace('.' . $fileInstanse->extension, '', $fileInstanse->name);
$fileName = StringHelper::truncate(Inflector::slug($baseName), 32, '');
if ($namePostfix || !$fileName) {
$fileName .= ($fileName ? '-' : '') . substr(uniqid(md5(rand()), true), 0, 10);
}
$fileName .= '.' . $fileInstanse->extension;
return $fileName;
}
示例7: fields
/**
* @inheritDoc
*/
public function fields()
{
return ['lastMessage' => function ($model) {
return ['text' => StringHelper::truncate($model['lastMessage']['text'], 20), 'date' => static::formatDate($model['lastMessage']['created_at']), 'senderId' => $model['lastMessage']['sender_id']];
}, 'newMessages' => function ($model) {
return ['count' => count($model['newMessages'])];
}, 'contact' => function ($model) {
return $model['contact'];
}, 'loadUrl', 'sendUrl', 'deleteUrl', 'readUrl', 'unreadUrl'];
}
示例8: beforeSave
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$settings = Yii::$app->getModule('admin')->activeModules['news']->settings;
if ($this->short && $settings['enableShort']) {
$this->short = StringHelper::truncate($this->short, $settings['shortMaxLength']);
}
return true;
} else {
return false;
}
}
示例9: beforeSave
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if ($insert == true) {
$this->status = StatusEnum::STATUS_ON;
}
$this->updateRevision();
$this->short = StringHelper::truncate(empty($this->short) ? strip_tags($this->content) : $this->short, 200);
return true;
} else {
return false;
}
}
示例10: beforeSave
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$settings = Yii::$app->getModule('admin')->activeModules['article']->settings;
$this->short = StringHelper::truncate($settings['enableShort'] ? $this->short : strip_tags($this->text), $settings['shortMaxLength']);
if (!$insert && $this->image != $this->oldAttributes['image'] && $this->oldAttributes['image']) {
@unlink(Yii::getAlias('@webroot') . $this->oldAttributes['image']);
}
return true;
} else {
return false;
}
}
示例11: formatConversation
/**
* @inheritDoc
*/
public function formatConversation($model)
{
$model['date'] = DateHelper::formatConversationDate($model['created_at']);
$model['text'] = StringHelper::truncate($model['text'], 20);
$model['new_messages'] = ArrayHelper::getValue($model, 'newMessages.count', 0);
$model['contact'] = ArrayHelper::merge($model['contact'], $model['contact']['profile']);
$model['deleteUrl'] = Url::to(['/' . $this->uniqueId . '/delete-conversation', 'contactId' => $model['contact']['id']]);
$model['readUrl'] = Url::to(['/' . $this->uniqueId . '/mark-conversation-as-read', 'contactId' => $model['contact']['id']]);
$model['unreadUrl'] = Url::to(['/' . $this->uniqueId . '/mark-conversation-as-unread', 'contactId' => $model['contact']['id']]);
$model['loadUrl'] = Url::to(['/' . $this->uniqueId . '/messages', 'contactId' => $model['contact']['id']]);
$model['sendUrl'] = Url::to(['/' . $this->uniqueId . '/create-message', 'contactId' => $model['contact']['id']]);
ArrayHelper::remove($model, 'contact.profile');
ArrayHelper::remove($model, 'newMessages');
return $model;
}
示例12: beforeSave
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$settings = Yii::$app->getModule('admin')->activeModules['article']->settings;
if ($this->short && $settings['enableShort']) {
$this->short = StringHelper::truncate($this->short, $settings['shortMaxLength']);
}
if (!$this->isNewRecord && $this->thumb != $this->oldAttributes['thumb']) {
@unlink(Yii::getAlias('@webroot') . $this->oldAttributes['thumb']);
}
return true;
} else {
return false;
}
}
示例13: generateFileInfo
/**
* @param string $file
* @param string $contents
* @param string $basePath
* @param string $baseUrl
* @return array
*/
protected function generateFileInfo($file, $contents, $basePath, $baseUrl)
{
// create file entry
if (preg_match('~<h1>(.*?)</h1>~s', $contents, $matches)) {
$title = str_replace('¶', '', strip_tags($matches[1]));
} elseif (preg_match('~<title>(.*?)</title>~s', $contents, $matches)) {
$title = strip_tags($matches[1]);
} else {
$title = '<i>No title</i>';
}
if (preg_match('~<div id="classDescription">\\s*<strong>(.*?)</strong>~s', $contents, $matches)) {
$description = strip_tags($matches[1]);
} elseif (preg_match('~<p>(.*?)</p>~s', $contents, $matches)) {
$description = StringHelper::truncate(strip_tags($matches[1]), 1000, '...', 'UTF-8');
} else {
$description = '';
}
return ['u' => $baseUrl . str_replace('\\', '/', substr($file, strlen(rtrim($basePath, '\\/')))), 't' => $title, 'd' => $description];
}
示例14: getMeta
public static function getMeta($arr = null)
{
if ($arr) {
?>
<title><?php
echo Html::encode($arr->title);
?>
| kotmonstr.com | артефакты прошлого</title>
<meta name="description"
content="<?php
echo Html::encode(StringHelper::truncate(isset($arr->content) ? $arr->content : $arr->title, 200));
?>
">
<meta name="keywords"
content="Артефакты прошлого , альтернативная история , ведическая культура , ведическая библиотека">
<?php
} else {
$module = Yii::$app->controller->module->id;
$controller = Yii::$app->controller->id;
$action = Yii::$app->controller->action->id;
$url = $module . '/' . $controller . '/' . $action;
$meta = Seo::getPageMeta($url);
//vd($meta);
?>
<title><?php
echo $meta ? Html::encode($meta->title) : '| kotmonstr.com | артефакты прошлого<';
?>
</title>
<meta name="description"
content="<?php
echo is_object($meta) ? Html::encode($meta->description) : 'kotmonstr.com | артефакты прошлого';
?>
">
<meta name="keywords"
content="<?php
echo is_object($meta) ? Html::encode($meta->keywords) : 'kotmonstr.com | артефакты прошлого | альтернативная история | ведическая культура русов';
?>
">
<?php
}
}
示例15: getFeed
public function getFeed($mode)
{
$feedMethod = 'rss';
if ($mode == self::RSS) {
$feedMethod = 'rss';
} elseif ($mode == self::ATOM) {
$feedMethod = 'atom';
}
$feed = new \Zend\Feed\Writer\Feed();
$feed->setTitle(Yii::t('app', 'Introduce business'));
$feed->setDescription(Yii::t('app', 'Introduce business'));
$feed->setLink(Yii::$app->getRequest()->getHostInfo());
$feed->setFeedLink(Yii::$app->getRequest()->getAbsoluteUrl(), $feedMethod);
$feed->setGenerator('Admap', Yii::$app->version, Yii::$app->getRequest()->getHostInfo());
$feed->addAuthor(['name' => 'Jafaripur', 'email' => 'mjafaripur@yahoo.com', 'uri' => 'http://www.jafaripur.ir']);
$feed->setDateModified(time());
//$feed->addHub('http://pubsubhubbub.appspot.com/');
foreach ($this->getModel(50) as $adver) {
$entry = $feed->createEntry();
$entry->setId($adver['id']);
$entry->setTitle(Html::encode($adver['title']));
$entry->addCategory(['term' => Html::encode($adver['category']['name']), 'label' => Html::encode($adver['category']['name'])]);
$entry->setLink(urldecode(Adver::generateLink($adver['id'], $adver['title'], $adver['category']['name'], $adver['country']['name'], $adver['province']['name'], $adver['city']['name'], $adver['address'], $adver['lang'], true)));
/*$entry->addAuthor(array(
'name' => 'Paddy',
'email' => 'paddy@example.com',
'uri' => 'http://www.example.com',
));*/
$entry->setDateModified((int) $adver['updated_at']);
$entry->setDateCreated((int) $adver['created_at']);
$entry->setDescription(\yii\helpers\StringHelper::truncate(strip_tags($adver['description']), 140));
//$entry->setContent ($description);
$feed->addEntry($entry);
}
return $feed->export($feedMethod);
}