RE: [PHP] Displaying first 100 characters of an article
| From: | Lawrence dot Sheed at dfait-maeci dot gc dot ca | Date: | Wed, 04 Oct 2000 05:05:04 +0000 |
| Subject: | RE: [PHP] Displaying first 100 characters of an article | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-18520@lists.php.net to get a copy of this message | ||
Ahh, the benefits of cut n paste revealed :)
$retstring = $restring . xxx
means $retstring is appending the contents of retstring on the second time
around
change it to
$retstring = substr($stuff,0,125) . '...';
or better still, make it into a function:
function TrimString ($sString, $iLen = 125) {
if (strlen($sString )<$iLen) {
return ($sString);
}
else {
return substr($sString,0,$iLen) . '...';
}
}
and use
print TrimString ($stuff);
You can optionally set a length;
eg
print TrimString ($stuff,50);
note that this suffers from the bug that if the length is within 3 of the
125 (or your limit, then it will make the actual size longer than the
original. Which is how microsoft does it.
-----Original Message-----
From: Ben Weinberger [mailto:ben@digitalsmiths.com]
Sent: October 4, 2000 12:56 PM
To: Lawrence.Sheed@dfait-maeci.gc.ca; php-general@lists.php.net
Subject: RE: [PHP] Displaying first 100 characters of an article
Hi~
Everything works great except for one aspect. Each time the next article
is displayed, it takes the previous article's first 125 characters and
prepends them to the current article's first 125 characters. To see what I
mean, go to http://www.siualumni.com/news.php. Any
ideas? The code I
ended up using is:
<?
// connect to database here
$mySQL = "SELECT * FROM news order by date limit 4";
$results = mysql_query($mySQL);
while ($myrow=mysql_fetch_array($results)) {
$stuff = "$myrow[message]";
if (strlen($stuff)<125) {
$retstring = $retstring . $stuff;
}
else {
$retstring = $retstring . substr($stuff,0,125) . '...';
}
print "$retstring";
}
?>
Thanks,
-Ben
At 03:30 AM 10/3/2000 -0400, Lawrence.Sheed@dfait-maeci.gc.ca wrote:
>How about:
>
>$stuff = "my long string here";
>
>if (strlen($stuff)<100) {
> $retstring = $retstring . $stuff;
>}
>else {
> $retstring = $retstring . substr($stuff,0,100) . "...";
>}
>
>cheers,
>
>Lawrence.
>
>-----Original Message-----
>From: Ben Weinberger [mailto:ben@digitalsmiths.com]
>Sent: October 3, 2000 03:27 PM
>To: php-general@lists.php.net
>Subject: [PHP] Displaying first 100 characters of an article
>
>
>Hi~
>I'm pulling data out of a MySQL db and displaying it on a web page... the
>data is like a news article with a title and a body. Currently, I can
>display the title and I have it linked to a javascript pop-up window that
>displays the full article. I would like to add the first 100 characters of
>the article under the title as well... something like follows:
>
>Version 6.0 just Released
>We've worked hard to bring you a new and improved version 6.0 software
>package so that you....
>
>Is there a way to make those words under the title appear and only display
>the first 100 characters followed by a "..."?
>
>Thanks,
>-Ben
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>For additional commands, e-mail: php-general-help@lists.php.net
>To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net