Shorten the title to the closest word

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 headers using PHP or jQuery, how can I trim them to the nearest word and add ellipses? Can I specify the number of characters?

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

Edit - How can I do this for each of the headers? I really don't know how to pass each header to a string ...

thanks

+6
jquery php wordpress-theming
source share
6 answers

PHP ellipsis creation

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

Then this function can be used as follows:

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

+5
source share

It is easy to use the PHP function. Take a 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; } 

Then you can pass the text using a function, for example:

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

(I have not tested this, but it should work just fine.)

+9
source share

See 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> 
+1
source share

You are probably looking for wordwrap () .

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

Use $ break to break the string using the optional break parameter. If the section is set to TRUE, the string will always be wrapped to or to the specified width. Therefore, if you have a word that is larger than a given width, it breaks.

Refer to the function documentation on the php website for more examples.

+++

Another solution would be to divide the title into ' ' (space) with explode () and provide a restriction to say max 5 words, than disconnect the last element of the array with array_pop and finally attach them to implode () using ' ' ( this space) as glue. But this solution is not the best, as it will give you an ugly result if you have long words.

+1
source share

If you want to be within a certain 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 great.

Edit: turned into a function.

+1
source share

using some php and powerful 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 has an ellipsis plugin, you might want to learn it.

0
source share

All Articles