Home >Backend Development >PHP Tutorial >PHP中一个控制字符串输出的函数_PHP

PHP中一个控制字符串输出的函数_PHP

WBOY
WBOYOriginal
2016-06-01 12:41:37854browse

// php 中 一个控制字符串输出的函数(中英文),每行显示多少字数,避免英文的影响
// $str 字符串
// $len 每行显示的字数(汉字×2)


function rep($str,$len)

{   
    $strlen=strlen($str);   
    $i=0;
    $finstr="";
    $pos=0;
     
    while($i     {   
      $s1=substr($str,$i,1);
      $s2=ord($s1);
      if($s2>0xa0){
       
          $finstr.=substr($str,$i,2);  
          $pos+=2;
          $i+=2;
       
      }else{
           
          switch($s2){
             case 13:  
              $finstr.="
";
              $pos=0;
              break;
             case 10:                
              $pos=0;
              break;              
             case 32;              
                 $finstr.=" ";
                 $pos++;
                 break;
             default:                       
              $finstr.=htmlspecialchars($s1);  
              $pos++;     
              break;
          }       
           
          $i++;  
           
       } //if
     
    if($pos>=$len){     
          $finstr.="
";
          $pos=0;
    }




    }  //while
    return $finstr;   
}   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn