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
how to only display X amount of chars then have ...?
Old 01-30-2008, 01:16 PM how to only display X amount of chars then have ...?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Silly question but how can i make a function which only shows X amount of chars and then has ....


Actual text:

The Cat sat on the mat.

New text limited to 10 chars:

The Cat sa...

how can i make this?
__________________
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!
 
 
Register now for full access!
Old 01-30-2008, 02:13 PM Re: how to only display X amount of chars then have ...?
Skilled Talker

Posts: 94
Trades: 0
PHP Code:
$string 'The Cat sat on the mat.';

$NewString substr($string 010).'...'
__________________
Sell Templates? Try our
Please login or register to view this content. Registration is FREE
! See a live
Please login or register to view this content. Registration is FREE
ChadR is offline
Reply With Quote
View Public Profile
 
Old 01-30-2008, 02:21 PM Re: how to only display X amount of chars then have ...?
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
Truncating a word always looks a bit sloppy.

Best set a maximum character length then find last space so you always have a full word.
PHP Code:
 <?php 
$cutpos    
=    8// maximum  length of character string
$text    =    'the cat sat on the mat'
$output    =    substr($text,0,$cutpos);  //  find  position  of  last  space  in  extract
$lastSpace  =  strrpos($output,' '); //  use  $lastSpace  to  set  length  of  new extract  and  add  ...
echo  substr($output,  0,  $lastSpace)."..."?>
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 02-01-2008, 02:00 PM Re: how to only display X amount of chars then have ...?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Thanks sorry took so long to reply
__________________
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 02-01-2008, 04:58 PM Re: how to only display X amount of chars then have ...?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
For anyone else who is interested.

turned it into a nice function, which also checks if the string is less than the amount of chars so it dont add ... to short strings

(in the situation im using it its in a loop getting the strinfs from a database so it helps )

PHP Code:
<?php
 
function shorten_txt($text$cutpos)
  {
 
# $cutpos    =    8; // maximum  length of character string
 # $text    =    'the cat sat on the mat'; 
      
if(strlen($text) > $cutpos)
   {
   
$output    =    substr($text,0,$cutpos);  //  find  position  of  last  space  in  extract
   
$lastSpace  =  strrpos($output,' '); //  use  $lastSpace  to  set  length  of  new extract  and  add  ...
   
return  substr($output,  0,  $lastSpace)."...";
   }
   else {
   return 
$text;
   }
        }
?>
__________________
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!
 
Reply     « Reply to how to only display X amount of chars then have ...?
 

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.21081 seconds with 12 queries