My favorites
▼
|
Sign in
bingu
bingu's projects
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
wp-plugins
/
automatic-excerpt.php
r22
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/*
Plugin Name: automatic excerpt
Plugin URI: http://wanwp.com/tips-tricks/automatic-excerpt-for-wordpress/
Description: 自动摘要显示。
Author: 冰古
Version: 1.0
Author URI: http://bingu.net
License: GNU General Public License 2.0 http://www.gnu.org/licenses/gpl.html
*/
$size = 5;
$more_link_text = 'more...';
add_action('the_content', 'control_content_size');
function control_content_size($content) {
global $size, $more_link_text;
if (is_singular()) return $content;
$content = strip_tags($content);
$content = cut_str($content, $size);
$content = '<p>' . $content . '<br /><a href="' . get_permalink() . "\" class=\"more-link\">$more_link_text</a></p>";
return $content;
}
/**
* cut_str用于截断包含中文(或其他多字节?)的utf8编码的字符串
* @param string $str utf8编码的字符串
* @param int $len 需要截取的长度(单位是字节)
*/
function cut_str($str, $len) {
if (!isset($str[$len])) {
// 判断字符串长度是否已经达到需要截取的长度
// 未达到时,直接输出原字符串
} else {
if (seems_utf8($str[$len-1])) // 判断截断字符串的最后一个字符是否是utf8编码的
$str = substr($str, 0, $len); // 如果是utf8编码的,直接截断输出
else { // 如果不是utf8编码的,因为utf8编码的中文是三个字节进行保存的,则判断该字符和周围字符组成的字符串是否符合utf8编码
if(seems_utf8($str[$len-3].$str[$len-2].$str[$len-1]))
$str = substr($str, 0, $len-3) . $str[$len-3] . $str[$len-2] . $str[$len-1];
elseif(seems_utf8($str[$len-2].$str[$len-1].$str[$len]))
$str = substr($str, 0, $len-2) . $str[$len-2].$str[$len-1].$str[$len];
elseif(seems_utf8($str[$len-1].$str[$len].$str[$len+1]))
$str = substr($str, 0, $len-1) . $str[$len-1].$str[$len].$str[$len+1];
else // 这个else应该不用也是可以的
$str = substr($str, 0, $len);
}
}
return $str;
}
/* EOF auto-excerpt.php */
/* ./wp-content/plugins/auto-excerpt.php */
Show details
Hide details
Change log
r13
by zhongygah on Sep 30, 2009
Diff
add automatic-excerpt.php
Go to:
...wp-plugins/automatic-excerpt.php
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 2233 bytes, 56 lines
View raw file
Powered by
Google Project Hosting