2016-09-17 14 views
0
<echo $this->Text->truncate(strip_tags($news['Newspost']['article_' . $lang]), 200, array('ending' => ' ...', 'exact' => false, 'html' => true));> 

我们正在使用此功能在我们的主页上创建新闻邮件预览。 当newsposts包含太多CAPITALS或宽度较大的符号(@#...)时,truncate无法正常工作,并显示一条额外的行。截断字符串,在使用更多首字母时应该更短

解决方法之一就是缩短截断时间,但对于普通帖子看起来不太好。

什么是最好的方式去做这件事? 网页上大约有10-20个这样的帖子,当我们在网站上同时有很多用户(100-500)时很重要。 所以我不想添加一些会让网站速度变慢的奇怪现象。

enter image description here

回答

0
<?php 



    function count_capitals($s) { 
     return strlen(preg_replace('![^A-Z]+!', '', $s)); 
    } 

    function truncate_str($str, $limit=30) { 
     if($limit < 3) $limit = 3; 
     if(strlen($str) > $limit) return substr($str, 0, $limit - 3) . ' ...'; 
     return $str; 
    } 

    function showTextByCapitalPercent($percent,$str) { 
     $int_percent = (int)$percent; 
     // 0-24% capitals 
     if(in_array($int_percent,range(0,24))) { 
      return truncate_str($str, $limit=50)."\n"; 
     } 

     // 25-49% capitals 
     if(in_array($int_percent,range(25,49))) { 
      return truncate_str($str, $limit=40)."\n"; 
     } 

     // 50-74% capitals 
     if(in_array($int_percent,range(50,74))) { 
      return truncate_str($str, $limit=30)."\n"; 
     }  

     // 75-100% capitals 
     if(in_array($int_percent,range(75,100))) { 
      return truncate_str($str, $limit=20)."\n"; 
     } 
     return ''; 
    } 


    $str1 = "Lorem ipsum dolor sit Amet, consectetur adipiscing elit. "; 
    $str2 = "Fusce eu mauris libero. Morbi auctor lobortis ex, pulvinar fermentum massa. "; 
    $str3 = "Cras DOLOR IPSUM, CONGUE EU ornare VITAE, egestas SED URNA. "; 
    $str4 = "Nunc NEC urna MOLLIS, rutrum nisi eu, bibendum turpis."; 


    $percent_capitals_str1 = (count_capitals($str1)*100)/strlen($str1); 
    $percent_capitals_str2 = (count_capitals($str2)*100)/strlen($str2); 
    $percent_capitals_str3 = (count_capitals($str3)*100)/strlen($str3); 
    $percent_capitals_str4 = (count_capitals($str4)*100)/strlen($str4); 


    echo "<pre>"; 

    echo "capitals str1: ".$percent_capitals_str1."%\n"; 
    echo "capitals str2: ".$percent_capitals_str2."%\n"; 
    echo "capitals str3: ".$percent_capitals_str3."%\n"; 
    echo "capitals str4: ".$percent_capitals_str4."%\n"; 

    echo "\n-------------------------\n"; 

    echo "str1: ".showTextByCapitalPercent($percent_capitals_str1,$str1)."\n"; 
    echo "str2: ".showTextByCapitalPercent($percent_capitals_str2,$str2)."\n"; 
    echo "str3: ".showTextByCapitalPercent($percent_capitals_str3,$str3)."\n"; 
    echo "str4: ".showTextByCapitalPercent($percent_capitals_str4,$str4)."\n"; 



/* 
out 
capitals str1: 3.5087719298246% 
capitals str2: 2.6315789473684% 
capitals str3: 51.666666666667% 
capitals str4: 18.518518518519% 

------------------------- 
str1: Lorem ipsum dolor sit Amet, consectetur adipisc ... 
str2: Fusce eu mauris libero. Morbi auctor lobortis e ... 
str3: Cras DOLOR IPSUM, CONGUE EU ... 
str4: Nunc NEC urna MOLLIS, rutrum nisi eu, bibendum ... 
*/ 

?> 

从字符串获得资本百分比和百分比的大小

EX1应用截断:资本= 10%仅显示50个字符

EX2:首都= 21 %只显示33个字符....