<?php
include 'config.php';
$gksql='select * from gk';
$gkjg=mysql_query($gksql,$conn);
$gkrow=mysql_fetch_array($gkjg);
$gk[]=array("gk"=>$gkrow[gk_nr]);
$smarty->assign("gk",$gk);
$smarty->display("index.html");
?>判断$gk的长度是否达到100,把超出100的内容用“…”代替,应该怎么写才能写出这效果,求高手写全,不要提示下,希望能写全
如果是用“{$title|truncate:30:’…’}”这个的话,那如果把内容放入DIV设定一个宽的架子里,内容宽不够时会自动换行,一旦换行在这里设置的'…'就变成乱码了,怎样解决

解决方案 »

  1.   


    if(strlen($gk)>100)
    $gk=substr($gk,0,100).'...';
      

  2.   

    那我在
    <?php
    include 'config.php';
    $gksql='select * from gk';
    $gkjg=mysql_query($gksql,$conn);
    $gkrow=mysql_fetch_array($gkjg);
    $gk[]=array("gk"=>$gkrow[gk_nr]);
    $smarty->assign("gk",$gk);
    $smarty->display("index.html");
    ?>
    这里应该怎么写啊,我是新手,不会啊
      

  3.   

    第二句应该改成这个:
    $gk=substr($gk,0,97).'...';LZ将$gk内容print_r出来看一下结构,循环里面的内容截取就好了
      

  4.   

    <?php
    /**
    * Smarty plugin
    * @package Smarty
    * @subpackage plugins
    */
    /**
    * Smarty truncate modifier plugin
    *
    * Type: modifier<br>
    * Name: truncate<br>
    * Purpose: Truncate a string to a certain length if necessary,
    * optionally splitting in the middle of a word, and
    * appending the $etc string or inserting $etc into the middle.
    * @link http://smarty.php.net/manual/en/lang...r.truncate.php
    * truncate (Smarty online manual)
    * @author Monte Ohrt <monte at ohrt dot com>
    * @param string
    * @param integer
    * @param string
    * @param boolean
    * @param boolean
    * @return string
    */
    function smarty_modifier_truncate($_String, $_Length, $etc='...',$_Start = 0, $_Encode = 'UTF-8') {
        $_Length=floor($_Length/2);//以英文为标准
        $v = 0;
        if ($_Encode == 'UTF-8') {
            $_Pa ="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
            preg_match_all($_Pa, $_String, $_Rarray);
            $_SLength = count($_Rarray[0]);        if ($_SLength >= $_Length){
               for ($i = $_Start,$v=0; $i < $_SLength; $i++) {
                    if ($v >= $_Length * 2 - 1){
                        return $_RS . $etc;
                    }
                    $num=ord($_Rarray[0][$i]);
                    if ($num>129){
                        $v += 2;
                    }else{
                        $v++; 
                    }
                    $_RS .= $_Rarray[0][$i];
                } 
            }
        } else {
            $_Start  = $_Start * 2;
            $_Length = $_Length * 2;
            $_Len    = strlen($_String);        if ($_Len >= $_Length){
                for ($i = $_Start; $i < $_Len; $i++) {
                    if ($v >= $_Length - 1){
                        return $_Rstring . $etc;
                    }
                    if (ord(substr($_String, $i, 1)) > 129) {
                        $_Rstring .= substr($_String, $i, 2);
                        $v += 2;
                        $i++;
                    } else {
                        $_Rstring .= substr($_String, $i, 1);
                        $v++;
                    }
                }
            }
        }
        return $_String;
    }
    /* vim: set expandtab: */
    ?>
    重写了smarty 的插件,我这个等宽度取字符串的...如果想保留14个汉字,应该参数为28,相当于28英文字符