ThinkPHP中截断指定长度的中文字符串后用删节号代替

WBOY
Lepaskan: 2016-06-13 12:55:37
asal
949 orang telah melayarinya

ThinkPHP中截断指定长度的中文字符串后用省略号代替

在TP模板中获取字符串并截断,以前一直觉得很神奇,现在才知道是自己不懂。

开始不是很了解模板中可以直接调用方法,以为$this->assign('content',$res);指派后只能在模板中这样接受{$content},于是写各种原生PHP语句无效

<php>$title = {$content['title']}</php>
Salin selepas log masuk

后来高手指点才知道在标签中不需要{}也就是

//显示效果


参考文章:点击打开链接

<php>$title = $content['title']</php>
Salin selepas log masuk
于是终于拨开乌云见日开了
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
//放在Common文件夹下common.php中的获取中文字符串的方法
Salin selepas log masuk
<pre name="code" class="php">function abslength($str){   
       $len=strlen($str);   
       $i=0; $j=0;  
       while($i//注意TP中的编码都是utf-8,所以+3;如果是GBK改为+2 
              }   
              else  
              {   
               $i+=1;   
             }   
          $j++;
       }
       return $j;
   } 
Salin selepas log masuk
<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

//模板中的部分代码

Salin selepas log masuk
Salin selepas log masuk
  • echo substr('abc',0,2).'...'; $title = $content['title']; $length = abslength($title); if ($length>6) { {$content['title'] | String::msubstr=0,5,'utf-8',true} //参考的是底部的连接 } else { echo $content['title']; }
  • {$content['time']}

    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Salin selepas log masuk
    Label berkaitan:
    sumber:php.cn
    Artikel sebelumnya: 【实时项目进度贴】原创PHP社区论坛项目正在进行中!关注。o(∩_∩)o.哈哈。该如何解决 Artikel seterusnya: CodeIgniter警告:Cannot modify header information 请高手帮忙,该如何解决
    Kenyataan Laman Web ini
    Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
    Artikel terbaru oleh pengarang
    Isu terkini
    Topik-topik yang berkaitan
    Lagi>
    Cadangan popular
    Tutorial Popular
    Lagi>
    Muat turun terkini
    Lagi>
    kesan web
    Kod sumber laman web
    Bahan laman web
    Templat hujung hadapan