Jump to content

Archived

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

Dobakat

How do I do this while outputing info?

Recommended Posts

Well I got a website, and I got a function that helps me add the news to the MySql database. On the main page I use PHP to get the 2 newest news. And I put echo"$fetch->news"; Well the $fetch->news does not matter, its just the assigned variable that has the news. The problem is that some news are too long. I only want the first # words to appear, then skip a line and put a link that says 'Read more...' with a link to the article. I am not worrying about the linking, only on how to making it put only 4-5 line of the begining of the news. If I was confunsing in some way, let me know. I hope someone can answer my question.

Share this post


Link to post
Share on other sites
Make a function like this (it takes as params the article string and its id to build the link):


[code=php:0]
function fixLength($str, $id){
    $maxChars = 50;
    if (strlen($str)>$maxChars){
        $str= substr($str, 0, $maxChars )."...<br/><a href=\"index.php?article={$id}\">Read more &raquo;</a>";
    }
    return $str;
}
[/code]

Share this post


Link to post
Share on other sites
If you want a function that doesn't cut words, read the user notes for substr() over at [url=http://php.net/substr]http://php.net/substr[/url]

Share this post


Link to post
Share on other sites

×

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.