Home > Article > Backend Development > How to display ellipses in php when the string is too long
php method to display ellipses when a string is too long: 1. Use the strlen() function to determine the length of the string; 2. If the length of the string exceeds the specified length, use the substr() function to Extra strings are replaced with ellipses.
The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.
Due to work needs, we may need to replace the end parts of some strings with strings. So how can we achieve this with code?
In fact, it is very simple for us to replace the redundant string content with ellipses. We only need to use the functions provided by PHP reasonably. Let's take a look at how to implement it in code.
Code implementation:
/** * php显示指定长度的字符串,超出长度以省略号(...)填补尾部显示 * @ str 字符串 * @ len 指定长度 **/ function cutSubstr($str,$len=30){ if (strlen($str)>$len) { $str=substr($str,0,$len) . '...'; } return $str; }
Recommended learning: php training
The above is the detailed content of How to display ellipses in php when the string is too long. For more information, please follow other related articles on the PHP Chinese website!