限制单词的功能,然后在文本左侧显示

时间:2012-08-30 13:16:30

标签: php explode implode

我有一个带有更多按钮的div。 read more按钮展开文本,展开的文本位于其下方的第二个div中。完全卡住了。请帮忙。代码如下:

<?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>

3 个答案:

答案 0 :(得分:2)

试试这个:

<?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>

答案 1 :(得分:2)

上面的代码对我很有用。只想分享我的代码,我还添加了一些小代码来检查是否需要处理字符串。下面的代码将检查字符串计数,然后将字符串处理为array_splice,以确保提供的字符串需要缩短或缩短。

    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;
        }
    }

答案 2 :(得分:0)

你从哪里“获取”剩下的文字?

尝试这样的事情:

<?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>