How do I generate a summary or excerpt of each post?

2

I am wanting to build a php and javascript post system, I already know the admin will write the post, post the subject, category the date goes automatic and inserted in the database. This part I already know how to do, but wanted to know how I can display the posts in a summarized way on a page and when I click on the title of the post will load a page itself where you will have comments (which I already know) 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.

    
asked by anonymous 28.04.2015 / 02:19

4 answers

1

Another solution is to limit by words, using explode, 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.br');

Output

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed of eiusmod tempor inciidunt ut labore et dolore magna aliqua. Ut ... Read more

    
28.04.2015 / 15:06
1

You can use php's substr () (sub string) function to display a part of the content. See the usage examples in the function link.

An interesting observation is that if the content has html, it will count the tags. To avoid problems with 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
    
28.04.2015 / 03:22
1

You could create a 'summary' column in the table where a summarized content would be displayed. I do not recommend using substr () as it may end up giving spoiler and disinterested reader. Code to better exemplify:

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/pt_BR/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>');}

    
28.04.2015 / 03:29
1

A simple way is to summarize the text and then convert it 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: link

    
28.04.2015 / 14:23