Jump to content

Shorten String


lostprophetpunk

Recommended Posts

Hello,

 

I am working on a posting system. I have come across a problem.

 

I am trying to make it so that the main page only displays a set certain amount of words from the article, then add a '...' to the end. I have set it up so the full article will display on a different page. Below is the current code for my main page...

 

    echo "<div class='mainc'>\n";
    while($Data = mysql_fetch_array($res)){
    echo "<div class='maintop'>".$Data['article_name']."</div>\n";
    echo "<div class='main'>\n";
    echo "".$Data['entry']."\n";
    echo "</div>\n";
    echo "<div class='mainbottom'>#".$Data['article_id']." - Posted By <a href='#'>Matt</a> on ".$Data['date']." - <a href=\"article.php?article_id=".$Data['article_id']."\">Read More</a>\n";
    echo "</div>\n";
    }
    echo "</div><br /><br />\n";

 

I am looking for a way to shorten the ".$Data['entry']." (without permanently cutting down the size of the data) to a set certain length, and then add '...' to the end.

 

How would I go about doing this?

Link to comment
https://forums.phpfreaks.com/topic/120046-shorten-string/
Share on other sites

well, is this sort of what you are looking for?

 

$str = 'This is a long string just to test to see if I can use a temporary variable with ... at the end of it!';
echo $str . '<br />';
echo substr($str, 0, 25) . '...'; // change the 25 to whatever length you want. Since we are echoning this, we are not changing the actual string.. just its presentation

 

So basically, if your $Data['entry'] does exist and outputs a string.. simply use that variable instead.

Link to comment
https://forums.phpfreaks.com/topic/120046-shorten-string/#findComment-618464
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.