dansk Posted December 27, 2006 Share Posted December 27, 2006 Hello there,I am working on a news website and I would like be able to print on the front page only the first 40 or 50 words of the text that i retrieve from my DB, and if the reader wants to read more they can click on read more.Is there a speical command for that in php or do I have to write my own code for that?Thank you Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/ Share on other sites More sharing options...
Chronos Posted December 27, 2006 Share Posted December 27, 2006 I've used the function [url=http://www.php.net/manual/en/function.substr.php]substr[/url] for that one! Works for me! I'd suggest a code of something like this:[code]$output = substr($db_input,0,50)."... [<a href=''>Read more</a>]";[/code] Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/#findComment-148213 Share on other sites More sharing options...
dansk Posted December 27, 2006 Author Share Posted December 27, 2006 This actually works it sometimes cuts it the middle of words or do I have to create some kind of a field for Post_Summary and then out put it there?Thank you very much for the help Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/#findComment-148437 Share on other sites More sharing options...
alpine Posted December 27, 2006 Share Posted December 27, 2006 Try:[code]<?php$text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";$words = explode(" ", $text);$sentence = array();$max_words = 10;foreach($words as $word){ if(count($sentence) < $max_words) $sentence[] = $word; else break;}echo implode(" ",$sentence);?>[/code] Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/#findComment-148441 Share on other sites More sharing options...
dansk Posted December 27, 2006 Author Share Posted December 27, 2006 Hello alpine, I tried using it . no text would appear and I got this warning:Warning: implode() [function.implode]: Bad arguments. [code]$result=mysql_query("SELECT * FROM Announ.Articles WHERE Post_Status = 'Yes' ORDER BY ID DESC LIMIT 0, 5 ");while($news = mysql_fetch_array( $result )){echo "<b><h3><font = Arial ></b>";echo $news['Post_Title'];echo "</h3></font = Arial>";echo " Posted on: </h3>";echo $news['Post_Date'];echo "<br>";if($news["Image_Link"]==NULL) {$news["Image_Link"]='ISL_Logo.gif';} $mod_image=getimagesize($news["Image_Link"]); echo "<img src='".$news["Image_Link"]."''".imageResize($mod_image[0],$mod_image[1], 150)."'>";echo " ";//----------$words = explode(" ", $news['Post']);$sentence = array();$max_words = 150;foreach($words as $word){ if(count($sentence) < $max_words) $sentence[] = $word; else break;}echo implode(" ",$news['Post']);//------------ //$output = substr($news['Post'],0,150);//echo $output;echo "</br>";printf ("<a href=\"read_more.php?ID=%s\">%s</a>", $news['ID']," <h5><b> >>read more...</h5></b>");}?>[/code] Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/#findComment-148493 Share on other sites More sharing options...
trq Posted December 27, 2006 Share Posted December 27, 2006 At least make sure your query is working before trying to use the results from it.[code=php:0]$result=mysql_query("SELECT * FROM Announ.Articles WHERE Post_Status = 'Yes' ORDER BY ID DESC LIMIT 0, 5 ") or die(mysql_error());[/code] Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/#findComment-148496 Share on other sites More sharing options...
alpine Posted December 27, 2006 Share Posted December 27, 2006 [quote]echo implode(" ",$news['Post']);[/quote]You screwed it up man - you'r not suppose to implode the original one, there is nothing to implode there!Keep it [color=blue]echo implode(" ",$sentence);[/color] !! (try to see what it's doing) Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/#findComment-148498 Share on other sites More sharing options...
dansk Posted December 27, 2006 Author Share Posted December 27, 2006 You know Alpine, I actually figured that out on my car but it was too late for me. It works now,Can you explain that code segmant for me, it would help me alot to understand it as a new php learner.Thank you very much for your help guys Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/#findComment-148529 Share on other sites More sharing options...
trq Posted December 27, 2006 Share Posted December 27, 2006 Why not view the manual entry for [url=http://php.net/implode]implode[/url] you'll get an explination instantly. Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/#findComment-148533 Share on other sites More sharing options...
Nicklas Posted December 28, 2006 Share Posted December 28, 2006 Here´s another way of displaying the first 10 words:[code=php:0]<?php$text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";list($words) = explode(' |', preg_replace('/(.*?\s+){10}/s', '\\0|', $text));echo $words;?>[/code] Link to comment https://forums.phpfreaks.com/topic/31938-printing-summary-of-text/#findComment-148558 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.