Re: [PHP] string effect
At 9:51 PM +0100 2/29/08, Alain Roger wrote: What is the basic rule ? Text is cut off based on (numbers of words, number of characters,..) ? Yes. Use whatever you want. You can use the number characters or find the last *space* in a string that's just long enough to fit your limit. Let's say your limit is 100 characters. 1. First truncate the string to 100 characters. 2. Then search the string for the last space. 3. Then truncate the string at that point and add "...". It will be a good exercise for you. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string effect
Mr. Heyes more or less prompted me to go dig for my other, slightly heavier version, that doesn't chop words up: Sorry I hit Reply instead Reply All. Regardless, here's my str_curtail. There is a bug in it that means if the string is all one word then it gets curtailed to nada, but that's easily fixed. /** * Shortens the given string to the specified number of characters, * however will never shorten mid-word (going backwards to find white * space). Appends * "..." (unless third arg is given). * * @param string $strInput to shorten * @param int$length Length to shorten to (defaults to 35) * @param string $append String to append (defaults to "...") * @return string Resulting shortened string */ function str_curtail($str, $length = 35, $append = '...') { // String short enough already ? if (strlen($str) <= $length) { return $str; } $str = substr($str, 0, $length); // No body intentionally for ($i=$length - 1; !ctype_space($str{$i}) && $i > 0; --$i); return rtrim(substr($str, 0, $i)) . $append; } -- Richard Heyes Employ me: http://www.phpguru.org/cv -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string effect
On 2/29/08, Alain Roger <[EMAIL PROTECTED]> wrote: > Text is cut off based on (numbers of words, number of characters,..) ? > what is the algorithm for such thing ? Mr. Heyes more or less prompted me to go dig for my other, slightly heavier version, that doesn't chop words up: function breakUpLongLines( $text, $maxLength=96 ) { $counter = 0; $newText = ''; $array = array(); $textLength = strlen( $text ); for( $i = 0; $i <= $textLength; $i++ ) { $array[] = substr( $text, $i, 1 ); } $textLength = count( $array ); for( $x = 0; $x < $textLength; $x++ ) { if( preg_match( "/[[:space:]]/", $array[ $x ] ) ) { $counter = 0; } else { $counter++; } $newText .= $array[ $x ]; if( $counter >= $maxLength ) { $newText .= ' '; $counter = 0; } } return $newText; } -- Greg Donald http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string effect
On 2/29/08, Alain Roger <[EMAIL PROTECTED]> wrote: > Hi, > > since a long time now, i see that some paragraphs of text are cut off and > the additional text is replaced by 3 dots. > e.g: > > "this is the original long text but without any sense and also stupid" > > effect desired : > "this is the original long text..." > > this is usually used for title articles or some long paragraphs. > What is the basic rule ? > Text is cut off based on (numbers of words, number of characters,..) ? > what is the algorithm for such thing ? > > thanks a lot, function truncate( $string, $length=384, $ending='...' ) { if( strlen( $string ) > $length ) $string = substr( $string, 0, $length ) . $ending ; return $string; } -- Greg Donald http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] string effect
Hi, since a long time now, i see that some paragraphs of text are cut off and the additional text is replaced by 3 dots. e.g: "this is the original long text but without any sense and also stupid" effect desired : "this is the original long text..." this is usually used for title articles or some long paragraphs. What is the basic rule ? Text is cut off based on (numbers of words, number of characters,..) ? what is the algorithm for such thing ? thanks a lot, -- Alain Windows XP SP2 PostgreSQL 8.2.4 / MS SQL server 2005 Apache 2.2.4 PHP 5.2.4 C# 2005-2008