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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.