老师,是不是截取的字符串为27个而不是29个,貌似总是少两个???有这种情况吗???
代码没错。php的substr对英文数字有效,对汉字截取不准。
一个汉字在utf8状态下占三个字节,在gbk占两个字节,如果用substr截取的画,就得判断编码,然后再算出字节数。
也可以按照php的 mbstring扩展,使用mb_substr 这样就精确多了。相关内容可以百度一下。
贴一下你的代码
{testblock change='true' length=3}
{$str11}
{/testblock}
///////
<?php
require('../smarty/Smarty.class.php');
$smarty=new Smarty();
//五配置两方法
$smarty->left_delimiter="{";
$smarty->right_delimiter="}";
$smarty->template_dir="tpl";
$smarty->cache_dir="cache";
$smarty->compile_dir="template_c";
$smarty->assign('str11','01234567890123456789');
$smarty->display('block.tpl');
/////////////////////
<?php
function smarty_block_testblock($params,$content)
{
$change=$params['change'];
$length=$params['length'];
if($change=='true')
{
$content=str_replace(0,'A',$content);
}
$content=substr($content,0,$length);
return $content;
}
////////////////