龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

字符串截取 php按照指定长度截取字符串的代码

时间:2014-06-05 15:17来源:网络整理 作者:网络 点击:
分享到:
php按照指定长度截取字符串的代码 [代码片段(20行)]
<?php 
//if a string is longer than the defined length,  
//it will add 3 periods to the end of the string. 
//you can change it to what ever you want for example: 
//return substr($str,0,$len).'[<a href="read_more.html">Read More</a>]'; 
//http://www.outofmemory.cn
function strLength($str,$len){ 
    $lenght = strlen($str); 
    if($lenght > $len){ 
        return substr($str,0,$len).'...'; 
    }else{ 
        return $str; 
    } 
} 
$str = "This is a fairly long string."; 
//The fist part is the string, the second part is how long it can be 
echo strLength($str,10).'<br>'; 
echo strLength($str,30); 
?> 

精彩图集

赞助商链接