JakeSilver Posted September 15, 2008 Share Posted September 15, 2008 Hello There! I am making a small blog site for myself and instead of downloading packages i want to make it myself. I am wondering if there is a way to cut the number of words displayed.. For example, In blogs you usually have about 20 or 30 words displayed and then a link that says "Read More" Is it the query that cuts it to 30 words? Or is it a function? Can anyone shead some light on this please! Many Thanks, Jake Quote Link to comment https://forums.phpfreaks.com/topic/124292-solved-cutting-the-characters-displayed-mysql/ Share on other sites More sharing options...
Mchl Posted September 15, 2008 Share Posted September 15, 2008 You could use wordwrap() function for this Quote Link to comment https://forums.phpfreaks.com/topic/124292-solved-cutting-the-characters-displayed-mysql/#findComment-641842 Share on other sites More sharing options...
Garethp Posted September 15, 2008 Share Posted September 15, 2008 $Text = Split(" ", $Text); $Num = 30; //Number of words $Start = 1; $Total = ""; foreach ($Text as $v) { $Total = $Total . "" . $v . " "; $Start = $Start + 1; if($Start == $Num) { break; } } it could be off by one word, in which case, adjust start to make it right Quote Link to comment https://forums.phpfreaks.com/topic/124292-solved-cutting-the-characters-displayed-mysql/#findComment-641844 Share on other sites More sharing options...
JasonLewis Posted September 15, 2008 Share Posted September 15, 2008 You can do it in the query with SUBSTR, but this would do characters. Not sure if MySQL has a word limiter. Or if you want you could do it like so: function limit_words($text, $words){ $split = explode(" ", $text); for($i = 0; $i < $words; $i++){ $newText[] = $split[$i]; } return implode(" ", $newText); } echo limit_words("Hello there this should be reduced to two words!", 2); There is probably even a function that already does it for you. But there we are. Quote Link to comment https://forums.phpfreaks.com/topic/124292-solved-cutting-the-characters-displayed-mysql/#findComment-641847 Share on other sites More sharing options...
JakeSilver Posted September 15, 2008 Author Share Posted September 15, 2008 ohhh Thanks guys! lots of differnt options then Problem Solved! Quote Link to comment https://forums.phpfreaks.com/topic/124292-solved-cutting-the-characters-displayed-mysql/#findComment-641849 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.