【问题标题】:strip_tags|truncate:150 only return a few charsstrip_tags|truncate:150 只返回几个字符
【发布时间】:2013-02-06 12:54:01
【问题描述】:

我有一个职位搜索页面,它显示所有职位及其前 150 个字符的职位描述:

{$listing.JobDescription|strip_tags|truncate:150} in smarty php template file

将返回:

Responsibilities:  1) Handle...  

这就是上面的源代码:http://jsfiddle.net/e2ScF/126/ .

从编辑器构建的原始文本和示例文本位于:http://jsfiddle.net/e2ScF/125/

我的问题是如何在没有


等html标签的情况下显示求职结果:

Responsibilities:1) Handle outbound telesales campaigns for consumer markets (for CallMark clients) by...

【问题讨论】:

  • Truncate 还会剪切 html 标签。这不仅仅是剪切文本内容。
  • 它会像 jsfiddle.net/e2ScF/126 那样计算剪切的 html,或者为什么会发生这种情况?
  • 也许你应该在截断之前|trim(或者减少空格)
  • 我添加了 |trim|strip_tags|truncate:150 或 |strip_tags|trim|truncate:150 但没有任何改变...

标签: php html smarty truncate strip-tags


【解决方案1】:

您的问题是文本中间有很多空格,所以 truncate 将它们计入 150 个限制。

试试strip:

{$listing.JobDescription|strip_tags|strip|truncate:150}

【讨论】:

  • 谢谢。它有助于修复它。
【解决方案2】:

这是一个 PHP 示例 (See it in action):

<?php
    $string = '<p>
    &nbsp;</p>
<table align="center" border="0" cellpadding="3" cellspacing="0" class="normalword" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: rgb(0, 0, 0);" width="95%">
    <tbody>
        <tr>
            <td>
                <table align="center" border="0" bordercolor="#666666" cellpadding="3" cellspacing="0" class="normalword" width="100%">
                    <tbody>
                        <tr>
                            <td>
                                <div align="justify">
                                    Responsibilities:&nbsp;<br />
                                    <br />
                                    1) Handle outbound telesales campaigns for consumer markets (for CallMark clients) by utilizing effective presentation and creating positive relationship for all customer contacts with the aim to cross/up selling client&rsquo;s product and services.&nbsp;<br />
';

function firstXChars($string, $chars = 100)
{
    $string = trim(strip_tags($string));
    $string = str_replace(array("\n", "\r"), '', $string);
    preg_match('/^.{0,' . $chars. '}(?:.*?)\b/iu', $string, $matches);
    return $matches[0];
}

echo firstXChars($string);

【讨论】:

  • 嗨!我单击“查看实际操作”,发现输出就是我现在所拥有的......那是:'职责:1)处理'。 char 不应该从 Responsibilities 而不是

    开始计算吗?

猜你喜欢
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多