0
点赞
收藏
分享

微信扫一扫

截取字符串,后打点

乐百川 2022-11-11 阅读 101


    最近项目中经常用到截取字符串进行显示的问题,在此特意总结一下:

1.vm


#set($title = $xpath.getNode($item,”/rss/channel/item/title”).getText()) #set($titleShort = $title) #if($titleShort.length()>20) #set($titleShort = $titleShort.substr(0,20)+”…”) #end <a title=”$title”>$titleShort</a>

 

附: vm 中函数为 java

Java 中字符全部为 Unicode

 

 

2.php

 

function utf_substr($str,$len){
for($i=0;$i<$len;$i++){
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127){
$i++;
if($i<$len){
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}else{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str);
}

if (strlen($a['describ'])>300) {
$a['describ'] = utf_substr($a['describ'],200)."...";
}

echo $a['describ']

 

这样不会出现截取后半个字体的情况出现啦。


举报

相关推荐

0 条评论