ThinkPHP+Smarty模板中截取包含中英文混合的字串亂碼的解 thinkphp3.2 smarty thinkphp整合smarty thinkphp smarty i

WBOY
發布: 2016-07-29 08:52:35
原創
1041 人瀏覽過

好幾天沒寫博客了,其實有好多需要總結的,因為最近一直在忙著做項目,但是困惑了幾天的Smarty模板中截取包含中英文混合的字符串亂碼的問題,終於解決了,所以記錄下來,需要的朋友看一下:

出現亂碼的原因:

對於字符串的截取,truncate函數只適合英文用戶,對與中文用戶來說,使用truncate會出現亂碼,而且對於中文英文混合串來說,截取同樣個數的字串,實際顯示長度上卻不同,一個中文的長度大致相當於兩個英文的長度。此外,truncate不能同時相容於GB2312、UTF-8等編碼。

解決方法:自己寫一個擴展類別使用

ThinkPHP使用的smarty的truncate變數調節器所在的類別檔案位置:ThinkPHPLibraryVendorSmartyplugins,其中有一個就是我們自己有一個可以使用我們自己的寫一個來實現

檔名:modifier.smartTruncate.php

<?<span>php
</span><span>/*</span><span>*
 * 中英文多编码字符串截取
</span><span>*/</span><span>function</span> smartDetectUTF8(<span>$string</span><span>)
{
    </span><span>static</span><span>$result</span> = <span>array</span><span>();
    </span><span>if</span>(! <span>array_key_exists</span>(<span>$key</span> = <span>md5</span>(<span>$string</span>), <span>$result</span><span>))
    {
        </span><span>$utf8</span> = "<span>            /^(?:
                [\x09\x0A\x0D\x20-\x7E]                            # ASCII
                | [\xC2-\xDF][\x80-\xBF]                             # non-overlong 2-byte
                | \xE0[\xA0-\xBF][\x80-\xBF]                       # excluding overlongs
                | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}           # straight 3-byte
                | \xED[\x80-\x9F][\x80-\xBF]                      # excluding surrogates
                | \xF0[\x90-\xBF][\x80-\xBF]{2}                 # planes 1-3
                | [\xF1-\xF3][\x80-\xBF]{3}                          # planes 4-15
                | \xF4[\x80-\x8F][\x80-\xBF]{2}                  # plane 16
            )+$/xs
        </span>"<span>;
        </span><span>$result</span>[<span>$key</span>] = <span>preg_match</span>(<span>trim</span>(<span>$utf8</span>), <span>$string</span><span>);
    }
    </span><span>return</span><span>$result</span>[<span>$key</span><span>];
}
</span><span>function</span> smartStrlen(<span>$string</span><span>)
{
    </span><span>$result</span> = 0<span>;
    </span><span>$number</span> = smartDetectUTF8(<span>$string</span>) ? 3 : 2<span>;
    </span><span>for</span>(<span>$i</span> = 0; <span>$i</span> < <span>strlen</span>(<span>$string</span>); <span>$i</span> += <span>$bytes</span><span>)
    {
        </span><span>$bytes</span> = <span>ord</span>(<span>substr</span>(<span>$string</span>, <span>$i</span>, 1)) > 127 ? <span>$number</span> : 1<span>;
        </span><span>$result</span> += <span>$bytes</span> > 1 ? 1.0 : 0.5<span>;
    }
    </span><span>return</span><span>$result</span><span>;
}
</span><span>function</span> smartSubstr(<span>$string</span>, <span>$start</span>, <span>$length</span> = <span>null</span><span>)
{
    </span><span>$result</span> = ''<span>;
    </span><span>$number</span> = smartDetectUTF8(<span>$string</span>) ? 3 : 2<span>;
    </span><span>if</span>(<span>$start</span> < 0<span>)
    {
        </span><span>$start</span> = <span>max</span>(smartStrlen(<span>$string</span>) + <span>$start</span>, 0<span>);
    }
    </span><span>for</span>(<span>$i</span> = 0; <span>$i</span> < <span>strlen</span>(<span>$string</span>); <span>$i</span> += <span>$bytes</span><span>)
    {
        </span><span>if</span>(<span>$start</span> <= 0<span>)
        {
            </span><span>break</span><span>;
        }
        </span><span>$bytes</span> = <span>ord</span>(<span>substr</span>(<span>$string</span>, <span>$i</span>, 1)) > 127 ? <span>$number</span> : 1<span>;
        </span><span>$start</span> -= <span>$bytes</span> > 1 ? 1.0 : 0.5<span>;
    }
    </span><span>if</span>(<span>is_null</span>(<span>$length</span><span>))
    {
        </span><span>$result</span> = <span>substr</span>(<span>$string</span>, <span>$i</span><span>);
    }
    </span><span>else</span><span>    {
        </span><span>for</span>(<span>$j</span> = <span>$i</span>; <span>$j</span> < <span>strlen</span>(<span>$string</span>); <span>$j</span> += <span>$bytes</span><span>)
        {
            </span><span>if</span>(<span>$length</span> <= 0<span>)
            {
                </span><span>break</span><span>;
            }
            </span><span>if</span>((<span>$bytes</span> = <span>ord</span>(<span>substr</span>(<span>$string</span>, <span>$j</span>, 1)) > 127 ? <span>$number</span> : 1) > 1<span>)
            {
                </span><span>if</span>(<span>$length</span> < 1.0<span>)
                {
                    </span><span>break</span><span>;
                }
                </span><span>$result</span> .= <span>substr</span>(<span>$string</span>, <span>$j</span>, <span>$bytes</span><span>);
                </span><span>$length</span> -= 1.0<span>;
            }
            </span><span>else</span><span>            {
                </span><span>$result</span> .= <span>substr</span>(<span>$string</span>, <span>$j</span>, 1<span>);
                </span><span>$length</span> -= 0.5<span>;
            }
        }
    }
    </span><span>return</span><span>$result</span><span>;
}
</span><span>function</span> smarty_modifier_smartTruncate(<span>$string</span>, <span>$length</span> = 80, <span>$etc</span> = '...',<span>$break_words</span> = <span>false</span>, <span>$middle</span> = <span>false</span><span>)
{
    </span><span>if</span> (<span>$length</span> == 0<span>)
        </span><span>return</span> ''<span>;
    </span><span>if</span> (smartStrlen(<span>$string</span>) > <span>$length</span><span>) {
        </span><span>$length</span> -= smartStrlen(<span>$etc</span><span>);
        </span><span>if</span> (!<span>$break_words</span> && !<span>$middle</span><span>) {
            </span><span>$string</span> = <span>preg_replace</span>('/\s+?(\S+)?$/', '', smartSubstr(<span>$string</span>, 0, <span>$length</span>+1<span>));
        }
        </span><span>if</span>(!<span>$middle</span><span>) {
            </span><span>return</span> smartSubstr(<span>$string</span>, 0, <span>$length</span>).<span>$etc</span><span>;
        } </span><span>else</span><span> {
            </span><span>return</span> smartSubstr(<span>$string</span>, 0, <span>$length</span>/2) . <span>$etc</span> . smartSubstr(<span>$string</span>, -<span>$length</span>/2<span>);
        }
    } </span><span>else</span><span> {
        </span><span>return</span><span>$string</span><span>;
    }
}
</span>?>
登入後複製

注意:在判斷字元長度時,一個中文字元算1.0,一個英文字元算0.5,截斷字串出現參差不齊的情況.

使用方法:

{<span>$content</span>|smartTruncate:5:"..."}
登入後複製

ok,測試沒有問題有問歡迎指出

以上就介紹了ThinkPHP+Smarty模板中截取包含中英文混合的字符串亂碼的解決方案,包括了smarty模板,thinkphp方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板