01c06-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 27 行

PHP
27
字号
<?php// A function to truncate text to a length and indicate more is available.function truncate_text_nicely($string, $max, $moretext) {	// Only begin to manipulate if the string is longer than max	if (strlen($string) > $max) {		// Modify $max by removing the length of moretext to allow room		$max -= strlen($moretext);			// Snag only the appropriate part of the string.		$string = strrev(strstr(strrev(substr($string, 0, $max)), ' '));			// Add the moretext onto it:		$string .= $moretext;	}	// Return the string, whether it was modified or not.	return $string;}$str = 'It was a dark and stormy night when the Baron prepared his plane.';// Parse the string into values:$values = truncate_text_nicely($str, 35, '...');// Echo out the result, will display:  It was a dark and stormy night ...echo "<pre>{$values}</pre>";?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?