Creating a string exerpt elegantly
Posted on April 5th, 2004 in Code Repository | 6 Comments »
Shortening a chunk of text into something suitable for display is an exceptionally common task, this function provides a number of options for shortening text and URLs.
/**
* Chop a string into a smaller string.
*
* @author Aidan Lister <aidan@php.net>
* @version 1.1.0
* @link http://aidanlister.com/2004/04/creating-a-string-exerpt-elegantly/
* @param mixed $string The string you want to shorten
* @param int $length The length you want to shorten the string to
* @param bool $center If true, chop in the middle of the string
* @param mixed $append String appended if it is shortened
*/
function str_chop($string, $length = 60, $center = false, $append = null)
{
// Set the default append string
if ($append === null) {
$append = ($center === true) ? ' ... ' : ' ...';
}
// Get some measurements
$len_string = strlen($string);
$len_append = strlen($append);
// If the string is longer than the maximum length, we need to chop it
if ($len_string > $length) {
// Check if we want to chop it in half
if ($center === true) {
// Get the lengths of each segment
$len_start = $length / 2;
$len_end = $len_start - $len_append;
// Get each segment
$seg_start = substr($string, 0, $len_start);
$seg_end = substr($string, $len_string - $len_end, $len_end);
// Stick them together
$string = $seg_start . $append . $seg_end;
} else {
// Otherwise, just chop the end off
$string = substr($string, 0, $length - $len_append) . $append;
}
}
return $string;
}
An example of this function in action:
$longtext = "this is some really long text with long words that should be chopped"; $longlink = "http://thisisareally.longlink/with/lots/of/stupid/paths/"; // Chop at default length echo str_chop($longtext); echo "\n"; // Chop in the middle echo str_chop($longtext, 60, true); echo "\n"; // Chop a link echo str_chop($longlink, 40, true); echo "\n"; // Chop a link whirlpool style echo "<a href=\"$longlink\">" . str_chop($longlink, 40, true) . '</a>';
This would produce the following output:
this is some really long text with long words that shoul ... this is some really long text ... ds that should be chopped http://thisisareally ... f/stupid/paths/ <a href="http://thisisareally.longlink/with/lots/of/stupid/paths/">http://thisisareally ... f/stupid/paths/</a>
6 Responses
nice, now imagine your string contained special characters of the kind “њ”.
It would be great to have a function that considers these as one character each and therefore avoid returning strings like : “This is a substring &1 …”
I’m interested if you could find a solution. Please email me!
Hi: I came across your code to shorten strings and URLs. I need to figure out how to shorten a string, stopping it not at a certain number of characters, but rather at the first instance of a period and space (end of sentence) Let me explain: The string consists of many sentences. I want only the first sentence to appear. I want the funtion to read the string and stop at the first occurence of “. ” which would indicate the end of a first sentence. I do not want the first sentence to end in the middle of a word, so indicating the number of characters will not work. Can you help? Many thanks inadvance. Norman Brown
[Editor's Note: Search for the first occurence of a full stop using strpos(). Then substr() to get that portion of the string.]
ngb55@hotmail.com
you can do it like this:
$tmparr = explode(“. “, $string,1);
echo $temparr[0];
” nice, now imagine your string contained special characters of the kind “њ”.”
You don’t do that. You do the chopping first, then htmlentities(), because ‘ and њ in length is equal (don’t know if њ is ‘, but it’s just an example).
My question is similiar to ngb55@hotmail.com. I want to find the total number of .(periods) in a text. If the text have a certain number of .(periods), divide the number by 3 and end the sentence at the period. If not print the text. In short, I am trying to print 1/3 of the text if the . (periods) total a certain number. And I will add a hyperlink using the words (more details).
Thanks for the work you have shared. It will save me hours (days).
Bookmarked and blogged.