使用 strlen 限制字符
我试图限制标题中出现的字符数。
不确定我在这里做错了什么(我是新手!),但标题显示为完整长度并且“...”没有出现。
非常感谢任何帮助!
<? if ($params->get('itemid')) $itemid = '&Itemid='.$params->get('itemid'); else $itemid = ''; ?>
<? $title = $event->title ?>
<? $title = substr($title, 0, 20); ?>
<? if (strlen($title) == 20) $title .= '...' ; ?>
<? if (KRequest::get('get.view', 'string') == 'events' || isset($module)) : ?>
<h1 class="title" itemprop="name">
<a href="<?=@route('option=joomla_component&view=event&id='.$event->id.$itemid)?>" itemprop="url"><?=$event->title?></a>
</h1>
<? endif ?>
I am trying to limit the number of characters that appear in a title.
Not sure what I am doing wrong here (I'm a novice!), but the title appears full length and the '...' does not appear.
Any help much appreciated!
<? if ($params->get('itemid')) $itemid = '&Itemid='.$params->get('itemid'); else $itemid = ''; ?>
<? $title = $event->title ?>
<? $title = substr($title, 0, 20); ?>
<? if (strlen($title) == 20) $title .= '...' ; ?>
<? if (KRequest::get('get.view', 'string') == 'events' || isset($module)) : ?>
<h1 class="title" itemprop="name">
<a href="<?=@route('option=joomla_component&view=event&id='.$event->id.$itemid)?>" itemprop="url"><?=$event->title?></a>
</h1>
<? endif ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该在输出中使用 $title 而不是 $event->title 吗?
Shouldn't you be using $title and not $event->title in your output?
看2条评论。它应该让它发挥作用。
See the 2 comments. It should make it work.
我不确定这是否会回答您的问题,但也许您应该尝试以下代码:
主要问题是您正在转换 $title 变量并显示 $event->title 变量。此外,您正在寻找精确长度为 20 的字符串来添加“...”,但您应该将其添加到任何具有 20 个或更多字符的字符串中。
我希望它有帮助!
I'm not sure if this will answer your question, but maybe you should try this code:
The main problem is that you're transforming the $title variable and display the $event->title variable. Besides, you're looking for strings of an EXACT length of 20 to add the "...", but you should add it to any string with 20 or more characters.
I hope it helped!