Re: displaying certain number of character

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hey Philip you can use this function

function cuttext($text, $maxChars = 20, $tail = '')
{
$arrWords = explode(" ", $text);
$temp = "";
for ($i=0; $i<count($arrWords); $i++)
{
$temp = ($i == 0 ? $arrWords[$i] : " ". $arrWords[$i] );
if (strlen($temp) < $maxChars)
{
$returnStr = $temp;
} 
else 
{
return $returnStr.$tail;
}
}
return $returnStr;
}

Usage:
echo cuttext("Please visit my site", 15, $tail = '..');
This will echo "Please visit"

On 8/31/05, Philip Hallstrom <php@xxxxxxxxxxxxxxx> wrote:
> 
> > $words= "If length is given and is negative,
> > then that many characters will be omitted from
> > the end of string (after the start position has
> > been calculated when a start is negative). If start
> > denotes a position beyond this truncation, an empty
> > string will be returned. ";
> >
> > echo substr($words, 0, 50).".......";
> > ?>
> >
> >
> > I have been using this but how can I make sure I do not split a word in
> > half? - Finishing on a <space> would probably do it.
> 
> You could use wordwrap() to wrap lines at 50 characters and then just take
> the first line....
> 
> http://us3.php.net/wordwrap
> 
> -philip
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 


-- 
*** phpgurru.com <http://phpgurru.com> [A php resource provider] ***

\\\|///
\\ - - //
( @ @ ) PHP is too logical for my brain
+---oOOo-(_)-oOOo------------------------------------------+
| Mian Shafiq ur Rehman
| phpgurru.com <http://phpgurru.com> [A php resource provider]
| 107 B, New Town, Multan Road
| Lahore Pakistan
|
| Mobile: 0300 423 9385
|
| ooo0 http://www.phpgurru.com
| ( ) 0ooo E-Mail: rehmanms@xxxxxxxxx
+---\ (----( )------------------------------------------+
\_) ) /
(_/

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux