修剪标题到最近的单词

时间:2010-12-15 01:50:14

标签: php jquery wordpress-theming

比如说我有以下代码:

<h3>My very long title</h3>
<h3>Another long title</h3>

如果我想使用PHP或jQuery缩短这些标题,我怎样才能将它们修剪成最接近的单词并附加省略号?是否可以指定字符数?

<h3>My very long...</h3>
<h3>Another long...</h3>

修改 - 我怎样才能为每个头条新闻做到这一点?我真的不知道如何将每个标题传递给字符串......

由于

6 个答案:

答案 0 :(得分:9)

使用PHP函数很容易。看看这个例子:

function trim_text($input, $length) {

    // If the text is already shorter than the max length, then just return unedited text.
    if (strlen($input) <= $length) {
        return $input;
    }

    // Find the last space (between words we're assuming) after the max length.
    $last_space = strrpos(substr($input, 0, $length), ' ');
    // Trim
    $trimmed_text = substr($input, 0, $last_space);
    // Add ellipsis.
    $trimmed_text .= '...';

    return $trimmed_text;
}

然后,您可以使用以下函数传递文本:

  

trim_text('我的超长标题',10);

(我没有对此进行过测试,但它应该可以正常运行。)

答案 1 :(得分:5)

Creating an ellipsis in PHP

<?php
function ellipsis($text, $max=100, $append='&hellip;')
{
    if (strlen($text) <= $max) return $text;
    $out = substr($text,0,$max);
    if (strpos($text,' ') === FALSE) return $out.$append;
    return preg_replace('/\w+$/','',$out).$append;
}
?>

然后可以通过以下方式使用此功能:

<?php
$text = "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.";
echo ellipsis($text,100);
?>

jquery Auto Ellipsis

答案 2 :(得分:1)

请参阅此question

function wrap($string, $limit) {
  $wstring = explode("\n", wordwrap($string, $limit, "\n") );
  return $wstring[0] . '...';
}

编辑:(包括&lt; = $ limit check)

<?php
function wrap($string, $limit) {
    if (strlen($string) <= $limit) {
        return $string;
    }
    $wstring = explode("\n", wordwrap($string, $limit, "\n"));
    return $wstring[0] . '...';
}
?>
<h3><?php echo wrap('My very long title', 12); ?></h3>

答案 3 :(得分:1)

也许您正在搜索wordwrap()

string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )

使用$ break来使用可选的break参数来中断该行。 如果剪切设置为TRUE,则字符串始终在指定宽度处或之前包装。因此,如果你有一个大于给定宽度的单词,它就会分开。

查看php网站上的功能文档以获取更多示例。

+++

另一个解决方案是使用explode()' '(空格)分割标题并提供最多5个字的限制,而不是使用array_pop切断数组的最后一个元素并最终加入它们implode()使用' '(该空格)作为粘合剂。但是这个解决方案不是最好的,因为如果你有长话,它会给你带来难看的输出。

答案 4 :(得分:1)

如果您希望在特定长度内,这将有效。

function truncateString($string, $length, $append = '...') {
    $length -= strlen($append); // length of "..."

    $string = substr($string, 0, $length);
    $string = substr($string, 0, strrpos($string, ' '));
    $string .= $append;

    return $string;
}
echo truncateString('My very long title', 15);

经过测试,效果很好。

编辑:变成了功能。

答案 5 :(得分:0)

使用一些php和强大的正则表达式

function formatHeadline($headline){
    if(preg_match('/(\w+\s+){3}/', $subject, $match)){
        return $match[1]  . '...';
    }
    return $headline;
}

使用regex和jquery在javascript中应该可以使用相同的方法。

Jquery也有ellipsis插件,您可能需要查看它。