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


strip_tags|truncate:150 only return a few chars

我有一个职位搜索页面,显示所有职位,其前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...

您的问题是文本中间有很多空格,因此truncate将它们计算在150的限制内。

试带:

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

下面是一个PHP示例(请参阅实际操作):

<?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);