lostprophetpunk Posted August 17, 2008 Share Posted August 17, 2008 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? Quote Link to comment Share on other sites More sharing options...
marcus Posted August 17, 2008 Share Posted August 17, 2008 <?php $string = "i love it when <a href=\"bigpapa.php\">you call me big papa</a>"; echo strip_tags(substr($string, 0, 40)) . "..."; ?> Quote Link to comment Share on other sites More sharing options...
lostprophetpunk Posted August 17, 2008 Author Share Posted August 17, 2008 Right, I have tried placing that in my code, but it doesn't seem to work... $entrym = "".$Data['entry'].""; strip_tags(substr($entrym, 40, 100)); echo "$entrym...\n"; It just seems to echo the '...'. Quote Link to comment Share on other sites More sharing options...
marcus Posted August 17, 2008 Share Posted August 17, 2008 Lawl. You're just putting it in, it's not going to do anything. $entrym = strip_tags(substr($Data['entry'],0,100)); echo $entrym . "..."; Quote Link to comment Share on other sites More sharing options...
lostprophetpunk Posted August 17, 2008 Author Share Posted August 17, 2008 It still just outputs the '...' and nothing else. Quote Link to comment Share on other sites More sharing options...
marcus Posted August 17, 2008 Share Posted August 17, 2008 Then obviously $Data['entry'] does not exist. Quote Link to comment Share on other sites More sharing options...
lostprophetpunk Posted August 17, 2008 Author Share Posted August 17, 2008 It does exist, as if I were to echo that by itself, it would post the full article. Quote Link to comment Share on other sites More sharing options...
lostprophetpunk Posted August 17, 2008 Author Share Posted August 17, 2008 Can anyone help? The the field entry does exist. I have tried it just with that and it works fine, but when I try to shorten the string it doesn't show anything but the '...' at the end. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 17, 2008 Share Posted August 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.