Jump to content


How do I do this while outputing info?

  • Please log in to reply
2 replies to this topic

#1 Dobakat

  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 13 September 2006 - 12:55 AM

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.

#2 yaba

  • Members
  • PipPip
  • Member
  • 27 posts

Posted 13 September 2006 - 04:09 AM

Make a function like this (it takes as params the article string and its id to build the link):

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;

#3 Jenk

  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 13 September 2006 - 09:14 AM

If you want a function that doesn't cut words, read the user notes for substr() over at http://php.net/substr

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users