unidox Posted September 26, 2007 Share Posted September 26, 2007 How would I make a read more to a news post? Right now it echos everything in the news_message table. Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/ Share on other sites More sharing options...
GingerRobot Posted September 26, 2007 Share Posted September 26, 2007 I'd probably just show the first few words of a given article, which could be achieved by exploding by a space, and echoing the first few words. For example: <?php $str = 'Here is my news arcticle that i only want to show the first few words of'; $num_words = 3; $words = explode(' ',$str); $i = 0; $teaser = ''; while($i<$num_words){ $teaser .=' '.$words[$i]; $i++; } echo $teaser; ?> You'd then include a link after that to read the whole article. Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355876 Share on other sites More sharing options...
unidox Posted September 26, 2007 Author Share Posted September 26, 2007 thanks Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355893 Share on other sites More sharing options...
cooldude832 Posted September 26, 2007 Share Posted September 26, 2007 that is a nasty idea to explode that big string, instead way better idea is find the first punctuation mark that falls greater than X characters in (like 10ish). Then just echo out a cut string from 0-that mark, unless that mark is greater than Y characters (say y is 25ish) then it just shows the first Y characters. By exploding you going to make a lot of useless data. Exploding this post alone is like a 50 item array, vs a test for that comma, and then simply displaying that portion up to it. Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355899 Share on other sites More sharing options...
GingerRobot Posted September 26, 2007 Share Posted September 26, 2007 Indeed. But what happens when you get people who dont use punctuation? Edit: Whoops. Didn't read your post properly. Perhaps that would be a better solution. Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355900 Share on other sites More sharing options...
unidox Posted September 26, 2007 Author Share Posted September 26, 2007 is there anyway I can make it so it only allows like 100 letters? Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355964 Share on other sites More sharing options...
darkfreaks Posted September 26, 2007 Share Posted September 26, 2007 if($str>100) { echo "there are too many words"; } Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355966 Share on other sites More sharing options...
marcus Posted September 26, 2007 Share Posted September 26, 2007 Um, darkfreaks $str would have to be an integer for that to be true. strlen() would do what darkfreaks is showing. <?php $string = "i like to spam chats and have fun while randomly typing sentences using only my pointer fingers can this sentence have any more run ons?"; if(strlen($string) > 100){ $dot = " ..."; }else { $dot = ""; } echo substr($string,0,100) . $dot; ?> Will show: i like to spam chats and have fun while randomly typing sentences using only my pointer fingers can ... Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355970 Share on other sites More sharing options...
darkfreaks Posted September 26, 2007 Share Posted September 26, 2007 oh i see i forgot to check the length first thanks mc Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-356007 Share on other sites More sharing options...
unidox Posted September 27, 2007 Author Share Posted September 27, 2007 so for the $string I would put the news_message like $string = $mysql[news_message]; ? Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-356612 Share on other sites More sharing options...
marcus Posted September 27, 2007 Share Posted September 27, 2007 Correct. Quote Link to comment https://forums.phpfreaks.com/topic/70780-readmore/#findComment-356613 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.