Need to limit amount of text loaded into a cell

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cnl83
Forum Commoner
Posts: 44
Joined: Mon Sep 26, 2005 10:34 am

Need to limit amount of text loaded into a cell

Post by cnl83 »

I have a some news that im loading into a cell. You can see the actual page here. (take a look at the section towards the bottom that says Todays hot news). http://www.poshinteriors.com/09/news/

I only want to be able to display no more than 250 Characters. Does anyone know a nifty way to do this in a cell?
fastnews-code.php is the form that actually loads the text from a text document. Would it be eaiser to load the first 250 characters from the text file, or do it the way im doing it? If so..how :)

Code: Select all

   <td height="110"></td>
    <td colspan="2" rowspan="2" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" background="imported/news.gif">
      <!--DWLayoutTable-->
      <tr>
        <td width="32" height="38">&nbsp;</td>
        <td width="258">&nbsp;</td>
        <td width="11">&nbsp;</td>
      </tr>
      <tr>
        <td height="89">&nbsp;</td>
        <td valign="top"><p align="left"><?php
include_once( dirname(__FILE__) . '/fastnews-code.php' );
$fn = new fastNews();
echo $fn->display();
?></a> </p></td>
          <td>&nbsp;</td>
      </tr>
     
    </table></td>
Last edited by Benjamin on Wed Jun 17, 2009 7:25 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Need to limit amount of text loaded into a cell

Post by Benjamin »

:arrow: Moved to PHP - Code :?
User avatar
jgadrow
Forum Newbie
Posts: 22
Joined: Wed Jun 17, 2009 7:56 pm
Location: Cincinnati, Ohio
Contact:

Re: Need to limit amount of text loaded into a cell

Post by jgadrow »

One solution is to wrap the display line in a call to substr ().

Code: Select all

echo substr ($fn->display (), 0, 250);
This will return only the first 250 characters. If there are less characters, they will all be displayed and you will have wasted a little processing power by retrieving them in this manner. However, if you check it first with strlen () you're still wasting a function call so might as well just apply substr ().
Post Reply