老師,是不是截取的字符串為27個而不是29個,貌似總是少兩個???有這種情況嗎???

王利亞1990
2014-08-21
3 回答
舉報
0/150
提交
取消
老師,是不是截取的字符串為27個而不是29個,貌似總是少兩個???有這種情況嗎???
2014-08-21
舉報
2014-08-25
代碼沒錯。php的substr對英文數字有效,對漢字截取不準。
一個漢字在utf8狀態下占三個字節,在gbk占兩個字節,如果用substr截取的畫,就得判斷編碼,然后再算出字節數。
也可以按照php的 mbstring擴展,使用mb_substr 這樣就精確多了。相關內容可以百度一下。
2014-08-21
貼一下你的代碼
2014-08-21
{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;
}
////////////////