strip_tags | truncate:150只返回几个字符

时间:2013-02-06 12:54:02

标签: php html smarty truncate strip-tags

我有一个求职页面,显示所有工作,其中包含来自jobdescription的前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/

我的问题是如何在没有<的情况下显示求职结果。 p>,< br />和其他html标签一样:

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

2 个答案:

答案 0 :(得分:1)

这是一个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 :(得分:1)

你的问题是文本中间有很多空格,所以truncate会在150限制内计算它们。

尝试strip

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