You are here: Re: displaying first 50 characters of a mysql text field? « PHP Programming Language « IT news, forums, messages
Re: displaying first 50 characters of a mysql text field?

Posted by Geoff Berrow on 10/23/05 11:26

I noticed that Message-ID: <0dB6f.4$jW4.3@fe24.usenetserver.com> from
David Haynes contained the following:

>
>$str50 = substr($mystring, 0, 50);

I use this method in a function and if the text is truncated the
function adds '...more' at the end. The function also takes a url so
that the word 'more' can be a link to an expanded version. $length is
obviously the amount you to which you want to truncate. The function
also 'steps back' to avoid truncating in the middle of a word.

<?php
function truncate($value,$length,$url){
if(strlen($value)>$length){
$value=substr($value,0,$length);
$n=0;
while(substr($value,-1)!=chr(32)){
$n++;
$value=substr($value,0,$length-$n);
}
$value=$value." ...<a href='$url'><em>more</em></a>";
}
return $value;
}
//usage
echo truncate("the quick brown fox jumps over the lazy
dog",15,"http:www.example.com?id=someid");
?>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация