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
27
28
29
30
31
32
33
34
35
36
        /**
	 * 生成"dasf..."形式字符串
	 *
	 * @param string $string 字符源
	 * @param int $strlen  获取字符长度
	 * @return string
	 */
	function shortFor($string, $strlen = 20, $etc = '...', $keep_first_style = false){
 
	    $strlen = $strlen*3;
	    $string = trim($string);
	    if ( strlen($string) <= $strlen ) {
	        return $string;
	    }
	    $str = strip_tags($string);
	    $j = 0;
 
	    for($i=0;$i<$strlen;$i++) {
	        if(ord(substr($str,$i,1))>0xa0){ $i+=2; $j+=3;}
	        else {$j++;}
	    }
	    $rstr=substr($str,0,$j);
	    if (strlen($str)>$strlen ) {$rstr .= $etc;}
 
 
	    if ( $keep_first_style == true && ereg('^<(.*)>$',$string) )    {
	        if ( strlen($str) <= $strlen )    {
	            return $string;
	        }
	        $start_pos = strpos($string,substr($str,0,4));
	        $end_pos = strpos($string,substr($str,-4));
	        $end_pos = $end_pos+4;
	        $rstr = substr($string,0,$start_pos) . $rstr . substr($string,$end_pos,strlen($string));
	    }
	    return $rstr;
	}

4 Responses to PHP 字符串省略形式输出[兼容所有编... »

  1. Don 评论 2009-06-13 19:03

    小Joe

    請問你這個程式碼的表格是用外掛,還是語法所產生的,有空的話可以麻煩你教我一下嗎?

    謝謝﹗

    回复

    joe 回复 六月 13th, 2009 at 19:16

    :lol: 这是纯php语法

    回复

  2. Don 评论 2009-06-13 19:23

    我去後台看到了WP-Syntax http://wordpress.org/extend/plugins/wp-syntax/這個外掛了﹗我的意思是指這個 div ;)

    回复

    joe 回复 六月 13th, 2009 at 19:25

    :mad: 汗,那你随便找了一篇文章留言啊。。。。
    这个是插件

    回复

Leave a Reply

Email address is not published

You should say a Chinese word to pass spam check. If you can not input Chinese, just copy 你好 and paste them into comment text box.