php字符串截取代码
下面写了两个php 字符串截取函数,方法很简单,string要截取的字符串,sublen长度,$start开位置,代码如下:
- function my_sub_str($string, $sublen, $start)
- {
- $pa = "/fdddd";
- preg_match_all($pa, $string, $t_string);
- if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
- return join('', array_slice($t_string[0], $start, $sublen));
- }
- /**
- * 字符串截取 不加"..."
- */
- function my_sub_str_00($string, $sublen, $start)
- {
- $pa = "/sss/";
- preg_match_all($pa, $string, $t_string);
- return join('', array_slice($t_string[0], $start, $sublen));
- }
- //字符截取测试
- $string ="www.phpfensi.com中国web第一站";
- echo my_sub_str($string, 10, 0); //输出 www.phpfensi....