TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-07-2007, 11:21 PM   #1 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default [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..." 

Last edited by bluesaga : 09-08-2007 at 12:37 AM.
bluesaga is offline  
Reply With Quote
The Following 2 Users Say Thank You to bluesaga For This Useful Post:
Alan @ CIT (01-10-2008), danielneri (01-10-2008)
Old 09-07-2007, 11:26 PM   #2 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Heheh, easy.

Thanks :)
Haris is offline  
Reply With Quote
Old 09-08-2007, 12:15 AM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 09-08-2007, 12:33 AM   #4 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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.
bluesaga is offline  
Reply With Quote
Old 09-11-2007, 10:52 AM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-10-2008, 08:40 PM   #6 (permalink)
The Contributor
 
Join Date: Dec 2007
Location: Florida
Posts: 73
Thanks: 12
danielneri is on a distinguished road
Default

Hey great function been looking for a little tidbit like this for quite some time, it's perfect for my current project!
Send a message via AIM to danielneri
danielneri is offline  
Reply With Quote
Old 01-11-2008, 02:48 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by coiyeun2b View Post
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!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-11-2008, 11:06 AM   #8 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 21
Thanks: 0
Speeple is on a distinguished road
Default

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.
Speeple is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:44 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design