View Full Version : How to truncate a string and add ellipses
AshleyQuick
03-24-2005, 06:12 AM
I looked at substr_replace but couldn't figure out how to implement it into the following:
' . $news['title'][$i]. '
It would be nice to add ... after 100 characters
Ashley
Brandoe85
03-24-2005, 06:36 AM
This (http://www.codingforums.com/showthread.php?t=50086) Might help you along... :)
AshleyQuick
03-24-2005, 07:21 AM
I found that thread, which I should've mentioned. I still cannot figure out how to implement it into this however:
' . $news['title'][$i]. '
This (http://www.codingforums.com/showthread.php?t=50086) Might help you along... :)
delinear
03-24-2005, 09:47 AM
You mean like this?
echo substr_replace($news['title'][$i], '...', 100);
This will echo the truncated first 100 characters with an elipsis at the end (obviously if you want the 100 characters to include the '...' just change the last argument to 97).
AshleyQuick
03-24-2005, 07:18 PM
Works great. Just noticed something I should've thought of though...is there a way to show the ellipses only if the string exceeds 100 characters? If the string is under 100, the ellipses are disregarded (in other words).
Fou-Lu
03-24-2005, 08:26 PM
To alter, you just need to figure out the current length:
$strlen = strlen($news['title'][$i]);
if ($strlen > 100)
{
$str = substr_replace($news['title'][$i], '...', 100);
}
else
{
$str = $news['title'][$i];
}
echo $str;
My personal preference though would be to limit it by words, so you don't end up with chopped off words at the end :P
Kurashu
03-24-2005, 09:26 PM
My personal preference though would be to limit it by words, so you don't end up with chopped off words at the end :P
Speaking of which....I found an example of your code (which I use >_>) and find helpful.
if(strlen($Str) > $Length){
substr ($Str, 0, strrpos(substr($Str, 0, $Length), ' ')) . '...';
}
Although, I altered it into a function....
function ellipse($string, $length)
{
if(strlen($string) > $length)
{
return substr($string, 0, strrpos(substr($string, 0, $length), ' ')) . '...';
}
else
{ return $string; }
}
Fou-Lu
03-25-2005, 12:08 AM
Hey I remember that :P
Heh, that one doesn't include linking for the ... section. It would be super easy to add in there, and I probably don't need to say how, but if you wanted to use it as a 'more' type of link, its a simple alteration!
Looking good kurashu!
AshleyQuick
03-25-2005, 04:15 AM
This is perplexing. I keep getting parse errors. HELP!! :)
<?php
$strlen = strlen($news['title'][$i]);
if ($strlen > 100)
{
$str = substr_replace($news['title'][$i], '...', 80);
}
else
{
$str = $news['title'][$i];
}
?>
<?php
for ($i = 0; $i < sizeof($news['title']); $i++) {
echo '<a target="_top" href="' . $news['link'][$i] . '">' . $str; . '</a>';
}
?>
Scrowler
03-25-2005, 04:26 AM
echo '<a target="_top" href="' . $news['link'][$i] . '">' . $str; . '</a>';
change to
echo '<a target="_top" href="' . $news['link'][$i] . '">' . $str . '</a>';
AshleyQuick
03-25-2005, 06:41 PM
Hmm, still not working. The link is there but the title is empty (not populating). Any ideas?
<?php
$strlen = strlen($news['title'][$i]);
if ($strlen > 100)
{
$str = substr_replace($news['title'][$i], '...', 100);
}
else
{
$str = $news['title'][$i];
}
?>
echo '<a target="_top" href="' . $news['link'][$i] . '">' . $str . '</a>';
Fou-Lu
03-25-2005, 08:35 PM
The last bit of code you have, is that complete, as in everything thats there?
If it is, your missing your loop. Take your code from a couple posts back, and change it like so:
<?php
for ($i = 0; $i < sizeof($news['title']); $i++) {
$strlen = strlen($news['title'][$i]);
if ($strlen > 100)
{
$str = substr_replace($news['title'][$i], '...', 80);
}
else
{
$str = $news['title'][$i];
}
echo '<a target="_top" href="' . $news['link'][$i] . '">' . $str . '</a>';
}
?>
Now, this assumes your array is named $news['title']. It may not be, that is probably a good reason for, not an error, but lack of population.
Perhaps its more like:
$news = array(
array(title => 'title', date => 'date'),
array(title => 'title', date => 'date')
);
but I don't know that for sure. If it is, the code needs changing. Best thing to do, is directly before the loop, do a print_r($news); and post your output. Also, your chop probably won't work correctly with strlen > 100 with the 80 limit in your substr_replace. In any case, lets ignore that for now, figure out the population problem first.
AshleyQuick
03-25-2005, 09:51 PM
Works! Thanks all.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.