Function to Limit Words, then show left over text

2019-07-07 03:21发布

问题:

I have a div that has a read more button. The read more button expands text, the expanded text is in a second div below it. Complete stuck. Please help. Code below:

<?PHP

function limit_words($string, $word_limit){
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}

$text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.";

?>

<div class="slidewrap">
    <img src="images/slides/image.jpg" alt="Image Title" />
    <h3>Title</h3>
    <p><?PHP echo(limit_words($text,50)); ?></p>
    <p><a href="#" class="readmore"><img src="images/icons/readmore.png" alt="" /></a></p>
    <div class="readmorexpand">
        <p><?PHP echo($leftovertext); ?></p>
    </div>
</div>

回答1:

try this:

<?php
function limit_words($string, $word_limit){
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}

$text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.";

$start = limit_words($text,50);
$end = str_replace($start, '', $text);
?>

<div class="slidewrap">
    <img src="images/slides/image.jpg" alt="Image Title" />
    <h3>Title</h3>
    <p><?php echo $start; ?></p>
    <p><a href="#" onclick="document.getElementById('readmorexpand').style.display = '';" class="readmore"><img src="images/icons/readmore.png" alt="" /></a></p>
    <div class="readmorexpand" id="readmorexpand" style="display:none">
        <p><?php echo $end; ?></p>
    </div>
</div>


回答2:

The code above works great for me. Just want to share my code also I added small code to check if I need to process the string or not. The code below will check the string count before it will process the string to array_splice to be sure that the string supplied needs to be shorten or not.

    function limit_words($string, $word_limit){
        $words = explode(" ",$string);
        if(count($words)>$word_limit){
           return implode(" ",array_splice($words,0,$word_limit));
        }else{
           return $string;
        }
    }


回答3:

Where are you "getting" the remaining text from?

Try something like this:

<?PHP

function limit_words($string, $word_limit){
    $words = explode(" ",$string);
    $ar = array();
    $ar[] = implode(" ",array_splice($words,0,$word_limit));
    $ar[] = implode(" ",array_splice($words,$word_limit));
    return $ar;
}

$text = "Lorem ipsum dolor ... snip ...  eleifend tellus.";


/** $ar contains the text split at the 50th "word" */
$ar = limit_words($string, 50)

?>

<div class="slidewrap">
    <img src="images/slides/image.jpg" alt="Image Title" />
    <h3>Title</h3>
    <p><?PHP echo $ar[0]; ?></p>
    <p><a href="#" class="readmore"><img src="images/icons/readmore.png" alt="" /></a></p>
    <div class="readmorexpand">
    <p><?PHP echo $ar[1]; ?></p>
    </div>
</div>