Rolinda Strijker Rolinda Strijker - 6 months ago 27
PHP Question

News item read more button

I'm making a news block for a website. I want if a news message has more than 64 words it shows a read more button. And if it has less then 64 words that just the message is shown. Now I already have the part with the read more button, but i can't get the part that if it has less then 64 words that the read more button dissapears. So the read more button is visible whether it has less or more than 64 words.

This is the piece of code

<?php
$query="SELECT * FROM seo_nieuws ORDER BY id DESC LIMIT 1";
$stmt = $pdo->query($query);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
?>
<h4><?php echo $row['titel']; ?></h4>

<?php
}
?>
<?php
function limit_words($string, $word_limit){
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}
$start = limit_words($row['bericht'],64);
?>

<div class="nieuws-tekst"><?php echo $start; ?><?= '...' ?></div>
<a class="button" href="http://www.linkofwebsite.nl/laatste-nieuws/nieuwsbericht.php?id=<?php echo $row['id']; ?>">Lees meer</a>


Update:

<?php
function limit_words($string, $word_limit){
$limitReached = FALSE;
$words = explode(" ",$string);
$content = implode(" ",array_splice($words,0,$word_limit));
if (count($words) >= $word_limit) {
$limitReached = TRUE;
}

return array($limitReached, $content);
}
list($limitReached, $content) = limit_words($row['bericht'],64);
?>

<div class="nieuws-tekst">
<?= $content; ?>
<?php if ($limitReached): ?>
... <a class="button" href="http://www.linkofwebsite.nl/laatste-nieuws/nieuwsbericht.php?id=<?= $row['id']; ?>">Lees meer</a>
<?php endif ?>
</div>


This is what I got now after a reaction. But the content shows but the read more button does not.

Output after var_dump($row['bericht]);

string(817) "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.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. xercitation ullamco laboris nisi ut aliquip ex ea "

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

jbe jbe
Answer

as jitendrapurohit already said, you can update your function to count the words.

if $string is html you have to make sure your functions handles that properly. otherwise you might end up with invalid html.

for example

$string = '<div>some long text</div>';

then when you truncate it after 64 words the div will not be closed.

here's an updated version of @jitendrapurohit's function which will also remove html from the string.

<?php

function limit_words($string, $word_limit)
{
    $limitReached = false;
    $words = explode(' ', $string);
    if (count($words) >= $word_limit) {
        $limitReached = true;
    }

    $content = implode(' ', array_splice($words, 0, $word_limit));

    return [$limitReached, $content];
}

list($limitReached, $content) = limit_words($row['bericht'], 64);

?>

<div class="nieuws-tekst">
    <?= $content; ?>
    <?php if ($limitReached): ?>
        <a class="button" href="http://www.linkofwebsite.nl/laatste-nieuws/nieuwsbericht.php?id=<?= $row['id']; ?>">
            Lees meer
        </a>
    <?php endif ?>
</div>

update:
array_slice changes its input. so after calling $content = implode(" ",array_splice($words,0,$word_limit)); $words contains all words except the first 64. i updated the function.