开发者

Trim headline to nearest word

开发者 https://www.devze.com 2023-01-31 04:05 出处:网络
Say for example I have the following code: <h3>My very long title</h3> <h3>Another long title</h3>

Say for example I have the following code:

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

If I wanted to shorten these titles using PHP or jQuery, how can I trim them to the nearest word and append an开发者_高级运维 ellipsis? Is it possible to specify a character count?

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

Edit - How can I do this for each one of the headlines? I don't really have an idea as to how to pass each headline into a string...

Thanks


This is easy using a PHP function. Look at this example:

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;
}

You can then pass in text with a function like:

trim_text('My super long title', 10);

(I haven't tested this, but it should work perfectly.)


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

This function can then be used in the following way:

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


Maybe you're searching for wordwrap().

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

Use $break to break the line using the optional break parameter. If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart.

Check out function documentation on php's site for more examples.

+++

Another solution would be to split title by ' ' (a space) using explode() and provide a limit to say max 5 words, than cut off the last element of array using array_pop and finally joining them with implode() using ' ' (that space) as glue. But this solution is not the best as it will give you ugly output if you have long words.


If you want to be within a specific length, this will work.

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

Tested, works perfectly.

Edit: turned into function.


See this question:

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

Edit: (including <= $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>


using some php and the mighty regular expressions

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

The same method should be possible in javascript using regex and jquery.

Jquery also have the ellipsis plugin, you might want to look into it.

0

精彩评论

暂无评论...
验证码 换一张
取 消