TP扩展函数 msubstr 截取字符串不能显示省略号的bug. 之前用设置$suffix=true了,可是还不能显示bug, 去论坛问了问,没人回答, 看了下源码。 修改了下.能正常显示省略号。
官方的源码:
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
| /**
+----------------------------------------------------------
* 字符串截取,支持中文和其他编码
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param string $suffix 截断显示字符
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr"))
return mb_substr($str, $start, $length, $charset);
elseif(function_exists('iconv_substr')) {
return iconv_substr($str,$start,$length,$charset);
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
if($suffix) return $slice."…";
return $slice;
} |
自己修改过的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr"))
{ if($suffix)
return mb_substr($str, $start, $length, $charset)."...";
else
return mb_substr($str, $start, $length, $charset);
}
elseif(function_exists('iconv_substr')) {
if($suffix)
return iconv_substr($str,$start,$length,$charset)."...";
else
return iconv_substr($str,$start,$length,$charset);
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
} |
官方的两个if 都成立就执行截取了, 没有$suffix 的事了。 汗.. *-*!
修改下,自己加上判断省略号。 OK。 ^_^
ThinkPHP提供了灵活的静态文件生成功能,可以在输出模板的同时生成需要的静态文件.
使用:
$this->buildHtml('静态文件', '静态路径','模板文件');
静态路径如果留空的话 默认保存在HTML_PATH(默认的HTML_PATH路径位于项目目录下面的Html目录,如果没有的话手动创建)定义的路径下面,,静态文件可以随意设置,也可以包括路径,如果不存在的路径系统会自动创建。
看下TP的buildhtml的源文件Action.class.php:
1
2
3
| protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='',$charset='',$contentType='text/html') {
return $this->view->buildHtml($htmlfile,$htmlpath,$templateFile,$charset,$contentType);
} |
View.class.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
| +----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @htmlfile 生成的静态文件名称
* @htmlpath 生成的静态文件路径
* @param string $templateFile 指定要调用的模板文件
* 默认为空 由系统自动定位模板文件
* @param string $charset 输出编码
* @param string $contentType 输出类型
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
public function buildHtml($htmlfile,$htmlpath='',$templateFile='',$charset='',$contentType='text/html') {
$content = $this->fetch($templateFile,$charset,$contentType);
$htmlpath = !empty($htmlpath)?$htmlpath:HTML_PATH;
$htmlfile = $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX');
if(!is_dir(dirname($htmlfile)))
// 如果静态目录不存在 则创建
mk_dir(dirname($htmlfile));
if(false === file_put_contents($htmlfile,$content))
throw_exception(L('_CACHE_WRITE_ERROR_'));
return $content;
} |
fetch类部分:
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
57
58
59
60
61
| /**
+----------------------------------------------------------
* 加载模板和页面输出
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $templateFile 模板文件名 留空为自动获取
* @param string $charset 模板输出字符集
* @param string $contentType 输出类型
* @param string $display 是否直接显示
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
*/
public function fetch($templateFile='',$charset='',$contentType='text/html',$display=false)
{
$GLOBALS['_viewStartTime'] = microtime(TRUE);
if(null===$templateFile)
// 使用null参数作为模版名直接返回不做任何输出
return ;
if(empty($charset)) $charset = C('DEFAULT_CHARSET');
// 网页字符编码
header("Content-Type:".$contentType."; charset=".$charset);
header("Cache-control: private"); //支持页面回跳
//页面缓存
ob_start();
ob_implicit_flush(0);
if(!file_exists_case($templateFile))
// 自动定位模板文件
$templateFile = $this->parseTemplateFile($templateFile);
$engine = strtolower(C('TMPL_ENGINE_TYPE'));
if('php'==$engine) {
// 模板阵列变量分解成为独立变量
extract($this->tVar, EXTR_OVERWRITE);
// 直接载入PHP模板
include $templateFile;
}elseif('think'==$engine && $this->checkCache($templateFile)) {
// 如果是Think模板引擎并且缓存有效 分解变量并载入模板缓存
extract($this->tVar, EXTR_OVERWRITE);
//载入模版缓存文件
include C('CACHE_PATH').md5($templateFile).C('TMPL_CACHFILE_SUFFIX');
}else{
// 模板文件需要重新编译 支持第三方模板引擎
// 调用模板引擎解析和输出
$className = 'Template'.ucwords($engine);
require_cache(THINK_PATH.'/Lib/Think/Util/Template/'.$className.'.class.php');
$tpl = new $className;
$tpl->fetch($templateFile,$this->tVar,$charset);
}
$this->templateFile = $templateFile;
// 获取并清空缓存
$content = ob_get_clean();
// 模板内容替换
$content = $this->templateContentReplace($content);
// 布局模板解析
$content = $this->layout($content,$charset,$contentType);
// 输出模板文件
return $this->output($content,$display);
} |
考虑的很全面.
TP很适合我这种懒人用。 ^_^
首先看一下ThinkPHP的控制器
ThinkPHP采用模块和操作的方式来执行
http://servername/appName/moduleName/actionName/params
执行的项目(appName)、模块(moduleName)和操作(actionName),appName可以不需要,通常是网站的首页,因为项目名称可以在入口文件中指定,这种情况下,appName就会被index.php替代 ,而ThinkPHP提供的URL_MODEL 的2模式 URL重写,可以去掉index.php,让你的URL看上去更友好。
阅读全文…
Recent Comments