It is necessary to display the first image of the article for previews - a picture and a little text.

For example, the body of the article:

<img src="files/1.jpg" alt="" width="200px" /> <img src="files/2.jpg" alt="" width="200px" /> <img src="files/3.jpg" alt="" width="200px" /> <p>Lorem ipsum dolor sit amet, consectetur adipiscing 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.</p> 

Here is the code that lists the thumbnails of the articles:

 while ($row = $results->fetchArray()) { echo 'id: ' . $row['id'] . '<br /> Дата и время: ' . $row['datetime'] . '<br /> Заголовок: ' . $row['title'] . '<br /> Категория: <a href="?category=' . $row['category'] . '">' . $row['category'] . '</a><br /> Контент: ' . mb_strimwidth($row['content'], 0, 300, '...') . '<br /> <a href="?article=' . $row['id'] . '">Читать</a> <br />-------------------------------------------------<br />'; } 

Help, kind people realize this thing!

In general, it is necessary to finish the regular book in such a way that it also displays the first available text up to 300 characters. And in regular expressions, I understand extremely tight :)

    1 answer 1

     <?php $texthtml = '<img src="files/1.jpg" alt="" width="200px" /> <img src="files/2.jpg" alt="" width="200px" /> <img src="files/3.jpg" alt="" width="200px" /> <p>Lorem ipsum dolor sit amet, consectetur adipiscing 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.</p>'; preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image); echo $image['src']; ?> 

    Result:

     files/1.jpg 

    In your example:

     while ($row = $results->fetchArray()) { preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $row['content'], $image); echo '<img src="'.$image['src'].'" alt="" width="200px" />'; // выводите вот этот кусок куда нужно echo 'id: ' . $row['id'] . '<br /> Дата и время: ' . $row['datetime'] . '<br /> Заголовок: ' . $row['title'] . '<br /> Категория: <a href="?category=' . $row['category'] . '">' . $row['category'] . '</a><br /> Контент: ' . mb_strimwidth($row['content'], 0, 300, '...') . '<br /> <a href="?article=' . $row['id'] . '">Читать</a> <br />-------------------------------------------------<br />'; } 

    PS: Try never to mix HTML, localization and logic (PHP, JS). It is desirable that everything was separate. Templates in one place, code in one and localization in another.

    • And how to push $ image into mb_strimwidth ($ row ['content'], 0, 300, '...')? - Linne
    • Sorry, of course, but for you all the work here will not do. This is not accepted in the community. The main thing you needed was a regular expression to pull the first image. And how and where to use the resulting $ image ['src'] - you decide. - Perfecto Web
    • Well, simply, $ row ['content'], so it already contains pictures, the regulars are clear to me, I don’t understand how to output. That's the problem. - Linne
    • Added to the answer. If the regular is clear, then copying the code seems easy. - Perfecto Web
    • Yes, I also understand this))) If you do this, for example:. $ image ['src']. mb_strimwidth ($ row ['content'], 0, 300, '...'), then displays it just over the content, plus all the other pictures of the article) In other words, it takes a picture from the content and adds it outside the content itself ... - Linne