Dobakat Posted September 13, 2006 Share Posted September 13, 2006 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. Quote Link to comment Share on other sites More sharing options...
yaba Posted September 13, 2006 Share Posted September 13, 2006 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 »</a>"; } return $str;}[/code] Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 13, 2006 Share Posted September 13, 2006 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] 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.