| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |

dedecms如何重新定義cn_substr函數截取的字數使其更準確?
不允許使用由dedecms的cn_substr()和cn_substr_utf8()截獲的字符串ms。我通常使用cn_substr()的習慣,今天不想使用任何cn_substr_utf8(),但它現在仍然更準確。按照一個漢字用兩個字節來稱呼就可以了。
推薦學習:夢想編織cms
方法描述:
首先,找到\ include \ helpers \ string.helper.php并替換原來的33-102行代碼(即定義cn_substr()函數的代碼)。如果你害怕它,你可以先備份這個文件,親愛的;
代碼如下:
/* * *截取的字符串為中英文,中文字符為2個字節* * @ access public * @ paramstring $ str要截取的字符串* @param int $cutLen截取的長度* @param bool $cutSlashes,您確定要刪除它嗎\ * @param bool $addSlashes是由\ * @param string $oDot截取后添加的字符串,例如三個常用的點* @ param bool $ hasHtml,是否有html * @return string */ if(!function _ exists(‘cn _ substr‘){ function cn _ substr($ str,$cutLen,$oDot = null,$ hasHtml = false,$cutSlashes = false,$ add slashes = false){ global $ CFG _ soft _ lang;$ str = trim($ str);if($ cutSlashes)$ str = strip slashes($ str);if($ hashhtml){ $ str = preg _ replace(“/(\ & lt;【^\<;】* \ & gt|\r|\n|\s|\【。+?\】)/是“,““,$ str);$ str = htmlspecialchars($ str);} else { $ str = htmlspecialchars($ str);} if($ cutLen & amp;& ampstrlen(str)& gt;$ cutLen){ $ nStr =“;if($ CFG _ soft _ lang = =‘utf-8′){ $ n = 0;$ TN = 0;$ NOC = 0;while($ n & lt;strlen($ str)){ $ t = ord($ str【$ n】);if($ t = = 9 | | $ t = = 10 | |(32 & lt;= $ t & amp& amp$ t & lt= 126)){ $ TN = 1;$ n++;$ noc++;} else if(194 & lt;= $ t & amp& amp$ t & lt= 223){ $ TN = 2;$ n+= 2;$ NOC+= 2;} else if(224 & lt;= $ t & amp& amp$ t & lt239){ $ TN = 3;$ n+= 3;$ NOC+= 2;} else if(240 & lt;= $ t & amp& amp$ t & lt= 247){ $ TN = 4;$ n+= 4;$ NOC+= 2;} else if(248 & lt;= $ t & amp& amp$ t & lt= 251){ $ TN = 5;$ n+= 5;$ NOC+= 2;} else if($ t = = 252 | | $ t = = 253){ $ TN = 6;$ n+= 6;$ NOC+= 2;} else { $ n++;} if($ NOC & gt;= $ cutLen)break;} if($ NOC & gt;$ cutLen)$ n-= $ TN;$ nStr = substr($ str,0,$ n);} else { for($ I = 0;$ i & lt$ cutLen–1;$ i++){ if(ord($ str【$ I】)& gt;127){ $ nStr。= $ str【$ I】。$ str【$ I+1】;$ i++;} else { $nStr。= $ str【$ I】;} } } $str = $nStr。$ oDot} if($ add slashes)$ str = add slashes($ str);$ str = htmlspecialchars _ decode($ str);return trim(str);}}第二,全站使用cn_substr()函數,不管你的程序是gbk還是utf8;
例如,如果要調用10個單詞(混合拼音和漢字):【field:title function =‘cn _ substr(@ me,20)‘】。
這幾篇文章你可能也喜歡:
- 清理緩存后,Dedecms尚未刷新頁面。如何解決?
- 如何將新用戶添加到DEDECMS如何設置用戶權限
- 為DEDECMS用戶分配權利的最佳實踐政策?
- DEDECM如何集成第三方統計工具(例如Youmeng)?
- DEWECMS移動適應的常見問題和解決方案?
本文由主機參考刊發,轉載請注明:如何通過dedecms重新定義cn_substr函數來更準確的截取字數? http://www.k2270.com/110554.html
主機參考

















評論前必須登錄!
注冊