TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   [Tip] Limiting a strings length (http://www.talkphp.com/tips-tricks/1050-tip-limiting-strings-length.html)

bluesaga 09-07-2007 11:21 PM

[Tip] Limiting a strings length
 
Below is a handy function for shortening a string, where space is needed...

PHP Code:

function limitString($szInput$iLength=25$szAffix='...')
{
   if(
strlen($szInput) <= $iLength)
      return 
$szInput;

   
$iLength $iLength strlen($szAffix);

   return 
trim(substr($szInput0$iLength)) . $szAffix);


Example:
PHP Code:

$string "This is a long string that should be shortened using our great function";

print 
limitString($string10'...');
//Outputs "this is..." 


Haris 09-07-2007 11:26 PM

Heheh, easy.

Thanks :)

Salathe 09-08-2007 12:15 AM

It's a very simple function, but I'm going to be picky (it's late here and this is what I do when I'm tired).
  • Why do you create $szOutput? It is assigned a value and immediately the next line is the return.
  • Speaking of the return, why is $szOutput trimmed just before returning if the string is limited, but not in any other case?
  • This is my main gripe, the actual length of the returned value (if the string has been trimmed) is $iLength + strlen($szAffix). The returned string should be of length $iLength, not greater. What's the point of limiting to, say, 20 characters when the actual string returned is 23?
  • The function, as is, has a syntax error -- line 6, the last parenthesis shouldn't be there.

bluesaga 09-08-2007 12:33 AM

bleh, way to be picky.

Anyway, will update the main post....

And the reason for $szOutput being assigned? simply for readability, this is for beginners remember.

PHP Code:

$iVariable = ($aVariable[1] > $aVariable[2]) ? ($iVar == 1) ? 0) : 0

Where as the above may be more efficient, its hardly the easiest code to read, especially for new PHP users. This is why im taking readability as a HUGE factor when writing these snippets, and things like not adding things assigning a variable before using it, is simply for ease of reading.

Wildhoney 09-11-2007 10:52 AM

Lol! Be nice to Salathe. He's only helping everyone out and that's why we're here. Perhaps we do need some further information on the ternary operator if you were going to use that in your function example.

danielneri 01-10-2008 08:40 PM

Hey great function been looking for a little tidbit like this for quite some time, it's perfect for my current project! :-)

Wildhoney 01-11-2008 02:48 AM

Quote:

Originally Posted by coiyeun2b (Post 8102)
Hi @bluesaga. I'm just test:
PHP Code:

//include your function
$str "Lol! Be nice to Salathe. He's only helping everyone out and that's why we're here.";
echo 
limitString($str50'...');
//output: Lol! Be nice to Salathe. He's only helping ever 

You can see, the word "everyone" had split to "ever" and "yone". I think you can improve function,it get the space near 50 (character) and split at this point. Output return like:

or

That would be lovely :-) I do have some functions in my various projects for this! But maybe you'd care to have a stab at it? Flaunt your stuff!

Speeple 01-11-2008 11:06 AM

Here's my effort:

PHP Code:

public static function concat($str$maxLen 220$noHTML false$ellipsis true) {
    if (
self::utf8_strlen($str) > $maxLen) {
        foreach (
explode(' '$str) as $word) {
            if ((
self::utf8_strlen($out) + self::utf8_strlen($word)) < $maxLen$out .= "$word ";

            else break;
        }

        return 
self::unicodeTrim($out',.-/:;''right') . ($ellipsis ? (!$noHTML ' <strong>…</strong>' ' …') : '');
    }

    else return 
$str;


Makes sure words aren't cut in half etc.

It does share the same issue as Will's function; the ellipsis isn't taken into consideration with $maxLen. I personally use the ellipsis symbol instead of 3 full stops.


All times are GMT. The time now is 01:45 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0