Function to Limit Words, then show left over text

余生长醉 提交于 2019-12-10 22:35:24

问题


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>


来源:https://stackoverflow.com/questions/12197756/function-to-limit-words-then-show-left-over-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!