Home >Backend Development >PHP Tutorial >PHP implementation code for truncating text at character breakpoints_PHP tutorial

PHP implementation code for truncating text at character breakpoints_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:30:131199browse

Copy the code The code is as follows:

//The so-called word break is where a word can be broken when changing lines. . This function will truncate the string at hyphenation points.
// 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, ' ');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323302.htmlTechArticleCopy code The code is as follows: //The so-called word break, that is, a word can be broken when changing lines. place. This function will truncate the string at hyphenation points. // Please acknowledge use of...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn