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

内容摘要
这篇文章主要为大家详细介绍了php按照指定长度截取字符串功能实例,具有一定的参考价值,可以用来参考一下。

对php按照指定长度截取字符串的代码对此感兴趣的朋友,看看idc笔记
文章正文

这篇文章主要为大家详细介绍了php按照指定长度截取字符串功能实例,具有一定的参考价值,可以用来参考一下。

对php按照指定长度截取字符串的代码对此感兴趣的朋友,看看idc笔记做的技术笔记!php按照指定长度截取字符串的代码,如果字符串超出了指定的长度,会用...替换,不过这段代码不支持中英文的区分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<code class="php">
/**
 * php按照指定长度截取字符串的代码
 *
 * @param
 * @arrange 512-笔记网: www.512PiC.com
 **/
<?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>]';
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);
 
/***   来自php教程(www.idcnote.com)   ***/</code>

注:关于php按照指定长度截取字符串功能实例的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!