PHP在字符斷點(diǎn)處截?cái)辔淖值膶?shí)現(xiàn)代碼
更新時(shí)間:2011年04月21日 22:54:34 作者:
所謂斷字 (word break),即一個(gè)單詞可在轉(zhuǎn)行時(shí)斷開的地方。這一函數(shù)將在斷字處截?cái)嘧址?/div>
//所謂斷字 (word break),即一個(gè)單詞可在轉(zhuǎn)行時(shí)斷開的地方。這一函數(shù)將在斷字處截?cái)嘧址?
// Please acknowledge use of this code by including this header.
function myTruncate($string, $limit, $break=".", $pad="...") {
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit)
return $string;
// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($string, $break, $limit))) {
if($breakpoint < strlen($string) - 1) {
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}
/***** Example ****/
$short_string=myTruncate($long_string, 100, ' ');
復(fù)制代碼 代碼如下:
//所謂斷字 (word break),即一個(gè)單詞可在轉(zhuǎn)行時(shí)斷開的地方。這一函數(shù)將在斷字處截?cái)嘧址?
// Please acknowledge use of this code by including this header.
function myTruncate($string, $limit, $break=".", $pad="...") {
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit)
return $string;
// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($string, $break, $limit))) {
if($breakpoint < strlen($string) - 1) {
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}
/***** Example ****/
$short_string=myTruncate($long_string, 100, ' ');
您可能感興趣的文章:
- C# double和decimal數(shù)據(jù)類型以截?cái)嗟姆绞奖A糁付ǖ男?shù)位數(shù)
- MSSQL 將截?cái)嘧址蚨M(jìn)制數(shù)據(jù)問題的解決方法
- oracle中截?cái)啾淼氖褂媒榻B
- js中根據(jù)字?jǐn)?shù)截取字符串,不能截?cái)鄒rl
- c#完美截?cái)嘧址a(中文+非中文)
- PHP UTF8中文字符截?cái)嗪瘮?shù)代碼
- PHP連接MSSQL時(shí)nvarchar字段長度被截?cái)酁?55的解決方法
- php使用iconv中文截?cái)鄦栴}的解決方法
- PHP截?cái)鄻?biāo)題且兼容utf8和gb2312編碼
- js實(shí)現(xiàn)文字截?cái)喙δ?/a>
相關(guān)文章
php引用計(jì)數(shù)器進(jìn)行垃圾收集機(jī)制介紹
每一種計(jì)算機(jī)語言都有自己的自動(dòng)垃圾回收機(jī)制,讓程序員不必過分關(guān)心程序內(nèi)存分配,php也不例外,但是在面向?qū)ο缶幊?OOP)編程中,有些對(duì)象需要顯式的銷毀;防止程序執(zhí)行內(nèi)存溢出2012-09-09PHP實(shí)現(xiàn)的構(gòu)造sql語句類實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)的構(gòu)造sql語句類,結(jié)合實(shí)例形式分析了PHP針對(duì)常用SQL語句的動(dòng)態(tài)構(gòu)造與生成技巧,需要的朋友可以參考下2016-02-02PHP結(jié)合vue導(dǎo)出excel出現(xiàn)亂碼的解決方法分享
這篇文章主要為大家詳細(xì)介紹了PHP結(jié)合vue導(dǎo)出excel出現(xiàn)亂碼的解決方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02php實(shí)現(xiàn)的樹形結(jié)構(gòu)數(shù)據(jù)存取類實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的樹形結(jié)構(gòu)數(shù)據(jù)存取類,實(shí)例演示了以樹形數(shù)據(jù)結(jié)構(gòu)存取數(shù)據(jù)的實(shí)現(xiàn)方法,對(duì)于學(xué)習(xí)基于PHP的數(shù)據(jù)結(jié)構(gòu)有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11php多進(jìn)程中的阻塞與非阻塞操作實(shí)例分析
這篇文章主要介紹了php多進(jìn)程中的阻塞與非阻塞操作,結(jié)合實(shí)例形式分析了php多進(jìn)程中的阻塞與非阻塞原理、阻塞控制方法與相關(guān)操作技巧,需要的朋友可以參考下2020-03-03