PHP implementation code for truncating text at character breakpoints_PHP tutorial

WBOY
Release: 2016-07-21 15:30:13
Original
1174 people have browsed it

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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!