Jump to content

printing summary of text


dansk

Recommended Posts

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

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]
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]
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.