Home > Web Development > How to trim text in PHP simply and effectively.

How to trim text in PHP simply and effectively.

Diego Cortés
Diego Cortés
January 20, 2025
How to trim text in PHP simply and effectively.

In the world of web development, it is common for us to need to present text concisely and in an organized manner. Trimming text in PHP is an essential skill that can enhance the presentation and readability of content on a website. Below are the most effective ways to do so using native PHP functions.

Why is it necessary to trim text?

Trimming text is important, especially when it comes to displaying summaries or excerpts on a website. An excessively long text can overwhelm the user, so being able to show only the most relevant information is fundamental. This not only helps the user experience but can also contribute to the site's SEO by keeping the content clear and to the point.

substr() Function

One of the simplest ways to trim text in PHP is by using the substr() function. This function allows you to specify the starting point and the length of the substring you want to obtain. Here is an example of its usage:

$texto = "This is an example of text that we are going to trim.";
$texto_recortado = substr($texto, 0, 25); // Trims to 25 characters
echo $texto_recortado; // Output: This is an example of 

Details about substr()

  • Parameters
    • The first is the original string.
    • The second is the starting index.
    • The third is the desired length.
  • Considerations: If the original text is shorter than the specified length, the entire string will be returned without error.

Using mb_substr()

To handle texts that contain multibyte characters (such as special characters from some languages), it is advisable to use mb_substr(). This function is part of the Multibyte String extension in PHP and works similarly to substr(), but it is designed to handle variable-length characters.

$texto = "Text with special characters: ñ, á, ü";
$texto_recortado = mb_substr($texto, 0, 30);
echo $texto_recortado; // Output: Text with special characters: ñ, á, ü

Advantages of mb_substr()

  • It allows for working with different languages, ensuring that special characters are respected.
  • It is ideal for applications that need to be multilingual.

Adding Ellipses

For better presentation, it is common to add ellipses at the end of the trimmed text to indicate that more content is available. This can be achieved through a simple concatenation:

$texto_recortado = mb_substr($texto, 0, 25) . '...';
echo $texto_recortado; // Output: Text with car... 

Simple Implementation

With a brief custom function, you can automate this procedure:

function recortarTexto($texto, $longitud) {
    return mb_substr($texto, 0, $longitud) . '...';
}

Practical Examples

Here are some situations where trimming text might be useful:

  • Article summaries: In a blog, showing a summary of 150 characters for each entry can be helpful.
  • Product listings: In e-commerce, offering a brief excerpt of the product description is essential.
  • Comments or testimonials: Displaying only the first few sentences of a testimonial can encourage readers to read more.

Conclusion

Trimming text in PHP is a straightforward task that can have a significant impact on the usability and presentation of your website. With functions like substr() and mb_substr(), you can achieve effective results quickly and easily.

If you’re looking for more content related to PHP and web development, I invite you to read more articles of this kind on my blog.

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 30.95 ms