Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Tip: A useful little function
Old 08-03-2008, 11:20 AM Tip: A useful little function
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
If you've ever found that you want to limit a string to a certain length and add an ellipsis (...) to it if it goes over that length, here is a useful little function for that:

PHP Code:
function ellipsis($string$length){
    if (
strlen($string) > $length) {
        
$string substr($string0$length).'...';
    }
    return 
$string;

I find it most useful for keeping information displayed in a table to a certain length so it doesn't mess up my layout. For instance, if you had a table that lists blog entries, you may want to use this on the title.

Usage

Both parameters are required for the function.
  • $string - the string to be shortened and to have an ellipsis added to
  • $length - the length at which the string should be shortened and an ellipsis should be added
Example

Using our blog example above, here is how to use this function:

PHP Code:
$blogTitle 'This is one example of a really long blog title that would normally probably be a dynamic entry';
$blogTitle2 'Short title';

echo 
ellipsis($blogTitle11); //Returns 'This is one...'
echo ellipsis($blogTitle211); //Returns 'Short title' 
That's it. Nothing too fancy or spectacular, but hopefully useful to someone. If you can improve it, please do so and post your changes. Feel free to use it wherever you need it.
__________________
Want new web resources every day? - Follow me on
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
 
Register now for full access!
Old 08-03-2008, 11:25 AM Re: Tip: A useful little function
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
I made a function like this a while ago i use it on a few of my sites, suprising how much u actaully need this kind of function on websites, in particular ones with user input

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-03-2008, 01:12 PM Re: Tip: A useful little function
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
I've also built one like this. I called it truncate().
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-03-2008, 07:06 PM Re: Tip: A useful little function
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
If you want the ellipses to break at a space, then this extension should help. I also added $lenght_is_max which allows you to require that the string returned is <= $length when the ellipses are counted.

PHP Code:
<?php
function ellipsis($string$length$length_is_max false){
    if (
strlen($string) > $length) {
        if (
$length_is_max) {
            
$last_space_character strrpos($string,' ', -1*$length 3);
        } else {
            
$last_space_character strrpos($string,' ', -1*$length);
        }
        
$string substr($string0$last_space_character).'...';
    }
    return 
$string;
}
echo 
ellipsis("Four score and seven years ago",15);  //Four score and...
echo '<br />';
echo 
ellipsis("Four score and seven years ago",15,true);  //Four score...

?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Tip: A useful little function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13876 seconds with 12 queries