PHP 在字符断点处截断文字的实现方法
内容摘要
这篇文章主要为大家详细介绍了PHP 在字符断点处截断文字的实现方法,具有一定的参考价值,可以用来参考一下。
所谓断字 (word break),即一个单词可在转行时断开的地方。这一函
所谓断字 (word break),即一个单词可在转行时断开的地方。这一函
文章正文
这篇文章主要为大家详细介绍了PHP 在字符断点处截断文字的实现方法,具有一定的参考价值,可以用来参考一下。
所谓断字 (word break),即一个单词可在转行时断开的地方。这一函数将在断字处截断字符串。PHP在字符断点处截断文字,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <code class = "php" > /** * 在字符断点处截断文字 * * @param * @author php教程 www.idcnote.com **/ // 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, ' ' ); /*** 代码来自php教程(www.idcnote.com) ***/ </code> |
注:关于PHP 在字符断点处截断文字的实现方法的内容就先介绍到这里,更多相关文章的可以留意
代码注释