Text before the “Details” button

Greetings. Since I found such a useful site, I will torture everyone here :).

I am displaying articles on my site. And in the database, you have to create specially two rows in the table – “Text” and “Description” (which goes before the button <<More>>). How to make it possible to display a certain number of characters or words from the “Text” (and not use a few extra lines in the database). I hope I clearly explained :).

Thanks in advance.


Answer 1

It is logical to assume that before the “Details” button there should be a whole paragraph (or several), and let’s say not the floor of the sentence, so you need to decide how many paragraphs you want to see before the book. “More”.

Next, run the script (where $textis the text of the article, $titleis the title of the article, $paragraph_countis the number of paragraphs before ” More”, suppose the paragraphs in the database are stored framed by the tag <p>...</p>):

$title = 'Some article';
$text = <<<TEXT
<p>These escaped characters are not very useful for outputting to a web page because     HTML ignore extra white space.</p> 
<p> A tab, newline, and carriage return are all examples of extra (ignorable) white     space.</p> 
<p>However, when writing to a file that may be read by human eyes these escaped     characters are a valuable tool!</p>
TEXT;
$paragraph_count = 3;
if (preg_match_all('/\<p\>.*?\<\/p\>/', $text, $matches))
{
    echo '  - '.$title.'<br />';   
    echo implode('', array_slice($matches[0], 0, $paragraph_count));
    echo '  >>>';
}

Answer 2

Somehow the question is written incorrectly, and you can’t understand what exactly you need. If I understand correctly:

echo mb_substr($sql['txt'],0,20,"UTF-8").'..'
 echo '<a href="#">...</a>';