php按照指定长度截取字符串功能实例

前端之家收集整理的这篇文章主要介绍了php按照指定长度截取字符串功能实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP按照指定长度截取字符串的代码感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
PHP按照指定长度截取字符串的代码,如果字符串超出了指定的长度,会用...替换,不过这段代码不支持中英文的区分

/**
 * PHP按照指定长度截取字符串的代码
 *
 * @param 
 * @arrange 512-笔记网: www.www.jb51.cc
 **/
<?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,$len).'[<a href="read_more.html">Read More</a>]'; 
function strLength($str,$len){ 
	$lenght = strlen($str); 
	if($lenght > $len){ 
		return substr($str,$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); 

/***   来自编程之家 jb51.cc(jb51.cc)   ***/

猜你在找的PHP相关文章