How to generate a summary or excerpt of each post?

Asked

Viewed 454 times

2

I want to build a php and javascript posting system, I already know that the admin will write the post, put the subject, category the date will automatic and inserted in the bank. This part I already know how to do, but wanted to know how I can display the posts summarized on a page and when I click on the title of the post will load a own page where will have comments ( I already know how to do ) and etc.

http://science.tumblr.com/post/116514430985/1-how-to-lewis-structure-it-up-in-here-by

The link would look like this, for example.

  • 2

    You can have a 'summary' field in the database where you would have a differentiated content to display as an abstract, or use php’s substrate() function to crop a part of the text and present it as a preview of the post in the listing. So the user would only read the full text by clicking on the title

  • 2

    Substrate?! It seems that your broker has pulled this off.

  • Hahahaha, damn programmers.

4 answers

1

You can use the function replace() (php sub string) to display a portion of the content. See examples of usage in the function link.

An interesting observation is that if the content has html, it will count the tags. To avoid problems in the display you can do as follows:

//consulta no banco
$semtags = strip_tags($row->conteudo); //remove as tags do php
$resumo = substr($semtags, 0, 255); //pega os primeiros 255 caracteres
  • 1

    Taking the opportunity to increase the answer of the colleague, the concept is to "truncate" the text, taking a number of characters. To get cooler, it’s worth blowing up the final string using space as a separator, deleting the last element, giving a Join in the array, and adding something like [...] at the end. The effect is great. : D

1

You could create a 'summary' column in the table where a summary content would be displayed. I do not recommend using substr() because it can end up giving spoiler and disinteresting the reader. Code to illustrate better:

try {
/** * 13.2.8 SELECT Syntax * https://dev.mysql.com/doc/refman/5.0/en/select.html */ $stmt = $PDO->query('select * from noticias order by id DESC LIMIT 5');

/**
 * setting the fetch mode
 * http://php.net/manual/en/pdostatement.setfetchmode.php
 */
$stmt->setFetchMode(PDO::FETCH_ASSOC);

while($row = $stmt->fetch()) { 
    echo ('
                <tr>
                    <td>'.$row['name'].'</td>
                    <td>'.$row['resume'].'</td>
                    <td>'.$row['date'].'</td>');}

1

A simple way is to summarize the text and then convert to a friendly url.

$texto = "codepad is an online compiler/interpreter, and 
a simple collaboration tool.Paste your code below, and 
codepad will run it and give you a short URL you can use
 to share it in chat or email.";

$resumo  = substr($texto, 0, 30);
$address = strtolower(trim(preg_replace('/[^a-zA-Z0-9]+/', '-', $resumo), '-'));

echo $address; // codepad-is-an-online-compiler

Example: http://codepad.org/1L1wHUDj

1


Another solution is to limit by words, using explodes, so you will not leave a word in half.

Function

function limit_words($string, $word_limit, $sus = TRUE, $link = '';) {
    $words = explode(' ',$string);
    return implode(' ',array_splice($words,0,$word_limit)).($sus ? '... <a href="'.$link.'">Leia mais</a>': '');
}

Example of use

$content = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

echo limit_words($content,20, TRUE, 'https://google.com');

Exit

Lorem ipsum dolor sit Amet, consectetur adipisicing Elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut... Read more

Browser other questions tagged

You are not signed in. Login or sign up in order to post.