Home > Backend Development > PHP Problem > How to display ellipses in php when the string is too long

How to display ellipses in php when the string is too long

王林
Release: 2023-03-12 16:46:01
Original
3769 people have browsed it

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.

How to display ellipses in php when the string is too long

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;
}
Copy after login

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!

Related labels:
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