[英]How to use character_limiter or word_limiter
我是PHP新手,無法為此找到解決方案:
我的網站正確打印了產品說明,有時可能會很長。 因此,我想將文本限制為例如100個單詞,然后顯示“閱讀更多...”,這將啟動一個Java腳本來顯示其余文本。
這是我目前擁有的東西,到目前為止,在沒有文本限制的情況下仍然可以正常工作:
<?php echo nl2br($property->description(null, null, true)); ?>
並建議我這樣做:
<?php $long_text = ($property->description(null, null, true)); // STRING!
$word_limit = 250; //your word limit, Int
$output = "";
$parts = explode( " ", $long_text );
foreach( $parts as $index=>$word ){
$output .= "$word ";
if( intval( $index ) >= intval( $word_limit ) ){
$output .= "READ MORE LINK";
break;
}
}
echo $output;
?>
結果,該腳本實際上將文本限制為250個單詞,但是
有人可以幫忙完成最后一部分嗎?
這是一個可以解決問題的簡單示例代碼
$long_text = "Your very long description"; // STRING!
$word_limit = 100; //your word limit, Int
$output = "";
$parts = explode( " ", $long_text );
foreach( $parts as $index=>$word ){
$output .= "$word ";
if( intval( $index ) >= intval( $word_limit ) ){
$output .= "READ MORE LINK";
break;
}
}
echo $output;
它的作用是建立一個單詞數組,當達到單詞數限制時,它會添加“閱讀更多鏈接”並退出循環,然后回顯輸出。 它不是最有效的解決方案,但我希望它能很好地解決您的問題,以幫助解決問題。
扭曲文字並走第一行
$text = "The quick brown fox jumped over the lazy dog.The quick brown fox jumped over the lazy dog.The quick brown fox jumped over the lazy dog.The quick brown fox jumped over the lazy dog.The quick brown fox jumped over the lazy dog.The quick brown fox jumped over the lazy dog.The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 100, "<br />\n");
$first_line=explode("<br />\n",$newtext);
echo $first_line[0];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.