php

PHP Shorten String With Three Dots

Using PHP, we can shorten the length of our string and just replace the rest with three dots.

This is usually needed in a software application, especially when the string length is too long to show to the user.

So we will write a function that is re-usable and which we can always call or use in any other programming.

 function trimString($string, $repl, $limit) 
{
  if(strlen($string) > $limit) 
  {
    return substr($string, 12, $limit) . $repl; 
  }
  else 
  {
    return $string;
  }
}
?>

 echo trimString("Hello Codeflare", "...", 3);

Explanation

The trimString function we created takes three (3) parameters:

  1. The string which we want to shorten,
  2. What we want to replace the cut out section with, which in this case is three (3) dots
  3. And the limit or length of string.

Next, we check if the length of the given string is greater than the limit which we have set. If it is, we truncate it and append our three dots. If it’s not, we just return the given string.

Conclusion

This is how we shorten the length of a string and replace it with three (3) dots in PHP.

Let me know what you think in the comments.

See Also:

Image Upload With Preview Using PHP’s PDO And jQuery

Enrol for Software Development Training

Recent Posts

Instagram Extends Reels Duration to 3 Minutes

Regardless of whether TikTok faces a U.S. ban, Instagram is wasting no time positioning itself…

2 days ago

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

6 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

7 days ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

1 week ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

1 week ago

Why Netflix Dumped React For its Frontend

Netflix, a pioneer in the streaming industry, has always been at the forefront of adopting…

1 week ago