我输入一个字符,和2个数字调用这个函数,结果老是0,不知哪里有错,望指正,谢谢function toRightString($string,$totalLength,$cnLength)//$totalLength总共显示的字符长度,$cnLength文字字符长度
  {
    $point=1;
if(!($cnLength%2==0))$cnLength++;
    $strLength=strlen($string);
if($cnLength<=$strLength)
{
   $string=substr($string,0,$cnLength);
   for($point;$point<=($totalLength-$cnLength);$point++)
   {
     $string+=".";
   }
 }else{
         for($point;$point<=($totalLength-$strLength);$point++)
 {
   $string+=".";
  }
   }
 return $string;     
  }

解决方案 »

  1.   

    <?php 
    function toRightString($string,$totalLength,$cnLength)//$totalLength总共显示的字符长度,$cnLength文字字符长度
      {
        $point=1;
        if(!($cnLength%2==0))$cnLength++;
        $strLength=strlen($string);
        if($cnLength<=$strLength)
        {
           $string=substr($string,0,$cnLength);
           for($point;$point<=($totalLength-$cnLength);$point++)
           {
             $string+=".";
           }
         }else{
                 for($point;$point<=($totalLength-$strLength);$point++)
                 {
                   $string+=".";
                  }
               }
         return    $string;     
      }
    echo toRightString("r","1","1");//r
    ?>
      

  2.   

    我随便测试了一下,如果第二个参数大于那个字符串的字符数的话,就会返回0
    例如:
    toRightString('r',2,1);
    就会返回0toRightString('rb',2,1);
    就不会
      

  3.   

    但你toRightString('rsafsdfsdfgdsfsdfss',12,8);照样输出0,这函数我是想到达输出类似
    sdasfds......,就是只显示字符串的其中一段,后面加上省略号的效果
      

  4.   

    建议使用mb_substr($str,0,10,'utf-8');
    国内超强ORM类http://code.google.com/p/queryphp/downloads/list 
      

  5.   

    谢谢各位,我知道哪里出问题了,果然是语法错误,$string+=".";这句明显错误啊,我把C,java的写法搬到这来了,哎呀,大意啊,正确写法应该是$string.=".";
    习惯了c的语法了,乱套了