本文整理汇总了PHP中Cake\Utility\Text::truncate方法的典型用法代码示例。如果您正苦于以下问题:PHP Text::truncate方法的具体用法?PHP Text::truncate怎么用?PHP Text::truncate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Utility\Text
的用法示例。
在下文中一共展示了Text::truncate方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: leadPlus
/**
* Show truncated long text and hide full version for flyout
*
* When there is a lot of text for a small area, this shows just the
* truncated lead. The full text is in an element that is hidden.
* Javascript or css can implement a flyout to show the full thing.
*
* Attributes:
* $filed.leadPlus =>
* div // div containing the hidden full text
* p // div > p containg the full text
* span // div + span containg truncated text
* truncate // [truncate][limit] charcter count in truncated text
*
* @param type $helper
*/
public function leadPlus($field)
{
if (!$this->hasUuid()) {
$uuid = new Uuid();
} else {
$uuid = $this->helper->entity->_uuid;
}
// established passed and default attributes
$passed_attributes = $this->helper->attributes("{$field}.leadPlus");
$default = new \Cake\Collection\Collection(['full_text' => ['span' => ['id' => $uuid->uuid('full'), 'class' => 'full_text', 'style' => 'cursor: pointer; display: none;', 'onclick' => 'var id = this.id.replace("full", "truncated"); this.style.display = "none"; document.getElementById(id).style.display = "inline-block";']], 'truncated_text' => ['span' => ['id' => $uuid->uuid('truncated'), 'class' => 'truncated_text', 'style' => 'cursor: pointer; display: inline-block;', 'onclick' => 'var id = this.id.replace("truncated", "full"); this.style.display = "none"; document.getElementById(id).style.display = "inline-block";']], 'truncate' => ['limit' => [100]], 'p' => []]);
// intermediary values for calculations
$key_template = $default->map(function ($value, $key) {
return null;
});
// this yeilds array with all keys. values will be int if passed in, null if default
$attribute_keys = array_flip(array_keys($passed_attributes)) + $key_template->toArray();
// get attributes to use during rendering
$attribute = $default->map(function ($value, $key) use($attribute_keys, $passed_attributes) {
if (is_null($attribute_keys[$key])) {
return $value;
} else {
return array_merge($value, $passed_attributes[$key]);
}
})->toArray();
// output
// SOMEONE SORT THIS OUT TO MAKE IT HAVE DEFAULT out-of-the-box BEHAVIOR
$hidden = $this->helper->Html->tag('span', $this->helper->entity->{$field}, $attribute['full_text']['span']);
$text = $this->helper->Html->tag('span', Text::truncate($this->helper->entity->{$field}, $attribute['truncate']['limit']), $attribute['truncated_text']['span']);
return $this->helper->Html->tag('p', $text . $hidden, $attribute['p']);
}
示例2: getDescription
/**
*
*/
public function getDescription($data, $entity, $options)
{
$content = '';
extract($options);
if (isset($fields)) {
if (is_array($fields)) {
foreach ($fields as $field) {
if ($entity->has($field)) {
$temp = preg_replace_callback("/(&#[0-9]+;)/", function ($m) {
return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES");
}, $entity->{$field});
$temp = preg_replace('/&[a-z]+;/', '', $temp);
$temp = preg_replace('/<\\/[a-z]+><[a-z]+>/', ' ', $temp);
$temp = preg_replace('/[\\s]{1,}/', ' ', $temp);
$content .= strip_tags($temp);
}
}
} else {
if ($entity->has($fields)) {
$temp = preg_replace_callback("/(&#[0-9]+;)/", function ($m) {
return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES");
}, $entity->{$fields});
$temp = preg_replace('/&[a-z]+;/', '', $temp);
$temp = preg_replace('/<\\/[a-z]+><[a-z]+>/', ' ', $temp);
$temp = preg_replace('/[\\s]{1,}/', ' ', $temp);
$content .= strip_tags($temp);
}
}
}
return Text::truncate($content, 160, ['ellipsis' => '', 'exact' => false, 'html' => false]);
}
示例3: findMap
/**
* Custom finder for map the ntofications.
*
* @param \Cake\ORM\Query $query The query finder.
* @param array $options The options passed in the query builder.
*
* @return \Cake\ORM\Query
*/
public function findMap(Query $query, array $options)
{
return $query->formatResults(function ($notifications) use($options) {
return $notifications->map(function ($notification) use($options) {
$notification->data = unserialize($notification->data);
switch ($notification->type) {
case 'conversation.reply':
$username = $notification->data['sender']->username;
$conversationTitle = Text::truncate($notification->data['conversation']->title, 50, ['ellipsis' => '...', 'exact' => false]);
//Check if the creator of the conversation is the current user.
if ($notification->data['conversation']->user_id === $options['session']->read('Auth.User.id')) {
$notification->text = __('<strong>{0}</strong> has replied in your conversation <strong>{1}</strong>.', h($username), h($conversationTitle));
} else {
$notification->text = __('<strong>{0}</strong> has replied in the conversation <strong>{1}</strong>.', h($username), h($conversationTitle));
}
$notification->link = Router::url(['controller' => 'conversations', 'action' => 'go', $notification->data['conversation']->last_message_id, 'prefix' => false]);
break;
case 'bot':
$notification->text = __('Welcome on <strong>{0}</strong>! You can now post your first comment in the blog.', \Cake\Core\Configure::read('Site.name'));
$notification->link = Router::url(['controller' => 'blog', 'action' => 'index', 'prefix' => false]);
$notification->icon = $notification->data['icon'];
break;
case 'badge':
$notification->text = __('You have unlock the badge "{0}".', $notification->data['badge']->name);
$notification->link = Router::url(['_name' => 'users-profile', 'id' => $notification->data['user']->id, 'slug' => $notification->data['user']->username, '#' => 'badges', 'prefix' => false]);
break;
}
return $notification;
});
});
}
示例4: _getBriefDescription
/**
* Gets a brief description of 80 characters long.
*
* @return string
*/
protected function _getBriefDescription()
{
$description = $this->get('description');
if (empty($description)) {
return '---';
}
return Text::truncate($description, 80);
}
示例5: api_fetch_all
public function api_fetch_all()
{
$this->autoRender = false;
// Objetos de tabela
$messages_table = TableRegistry::get("Messages");
// Sessão de admin
$user_id = $this->userLogged['user_id'];
// Se ele for um aluno
if ($this->userLogged['table'] == 'Users') {
}
// Busca todas as mensagens do usuário logado
$where = ['Messages.user_id' => $user_id];
$messages = $messages_table->find()->where($where)->contain(['MessageRecipients', 'MessageReplies'])->order(['id' => 'DESC'])->all()->toArray();
$atores_global = $this->getAtores();
// Itera todas as mensagens
foreach ($messages as $key_message => $message) {
// Data de envio formatada
$message->date = $message->created->format("d M");
// Destinatário
$message->to = [];
// Respostas
$message->replies = [];
// Autor
$message->from = "";
// Resumo
$message->excerpt = Text::truncate($message->content, 140, ['ellipsis' => '...', 'exact' => false]);
// Itera todos os atores
foreach ($atores_global as $ator_label => $atores) {
foreach ($atores as $ator) {
if ($ator->id == $message->model_id && $ator_label == $message->model) {
$message->from = $ator->full_name;
}
}
}
// Itera todos os recipientes e adiciona ao $to
foreach ($message->message_recipients as $message_recipient) {
foreach ($atores_global[$message_recipient->model] as $ator) {
if ($ator->id == $message_recipient->model_id) {
$message->to[] = $ator->full_name;
}
}
}
// Itera todas as respostas e adiciona a $replies
foreach ($message->message_replies as $reply) {
foreach ($atores_global[$reply->model] as $ator) {
if ($ator->id == $reply->model_id) {
$message->replies[] = ['content' => $reply->content, 'author' => $ator->full_name];
}
}
}
}
// Retorna em JSON
echo json_encode($messages);
}
示例6: writeTextOnImage
public function writeTextOnImage($fontSize, $fontFile, $fontColor, $text, &$img, $positionX, $positionY)
{
if (!$text) {
return false;
}
$text = preg_replace('/[?]+/', "", $text);
$text = \Cake\Utility\Text::wrap($text, 40);
$text = \Cake\Utility\Text::truncate($text, 80, ['exact' => false]);
$fontColor = $this->hex2rgb($fontColor);
$color = imagecolorallocate($img, $fontColor[0], $fontColor[1], $fontColor[2]);
$result = imagettftext($img, $fontSize, 0, $positionX, $positionY, $color, $fontFile, trim($text));
}
示例7: feed
public function feed()
{
$this->viewBuilder()->className('Feed.Rss');
$atomLink = array('controller' => 'articles', 'action' => 'feed', '_ext' => 'rss');
$articles = $this->Articles->find('all', ['fields' => ['title', 'slug', 'body'], 'order' => ['Articles.Created' => 'DESC']])->toArray();
foreach ($articles as $article) {
$article['link'] = 'http://moosecraft.us/articles/' . $article['slug'];
$article['body'] = Text::truncate($article['body'], 50, ['ellipses' => '...', 'exact' => false]);
unset($article['slug']);
}
$data = array('channel' => array('title' => 'Moosecraft', 'link' => 'http://moosecraft.us', 'description' => 'Latest News', 'atom:link' => array('@href' => $atomLink)), 'items' => $articles);
$this->set(array('data' => $data, '_serialize' => 'data'));
}
示例8: formatString
/**
* Format a string for display
*
* @param string $field Name of field.
* @param array $value Value of field.
* @return string
*/
public function formatString($field, $value)
{
return h(Text::truncate($value, 200));
}
示例9: testTruncateExactHtml
/**
* Test truncate() method with both exact and html.
* @return void
*/
public function testTruncateExactHtml()
{
$text = '<a href="http://example.org">hello</a> world';
$expected = '<a href="http://example.org">hell..</a>';
$result = Text::truncate($text, 6, ['ellipsis' => '..', 'exact' => true, 'html' => true]);
$this->assertEquals($expected, $result);
$expected = '<a href="http://example.org">hell..</a>';
$result = Text::truncate($text, 6, ['ellipsis' => '..', 'exact' => false, 'html' => true]);
$this->assertEquals($expected, $result);
}
示例10: testing
/**
* Just a testing method
*
* @return void
*/
public function testing()
{
Hash::extract();
$alias = Text::uuid();
$full = \Cake\Utility\Text::truncate($param1, $param2);
}