Home > Backend Development > PHP Tutorial > How Can I Precisely Truncate Multibyte Strings While Maintaining Word Boundaries?

How Can I Precisely Truncate Multibyte Strings While Maintaining Word Boundaries?

Linda Hamilton
Release: 2024-12-07 15:01:15
Original
616 people have browsed it

How Can I Precisely Truncate Multibyte Strings While Maintaining Word Boundaries?

Truncating Multibyte Strings with Precision

Introduction

Truncating strings to a specific character length is a common task in programming. However, when dealing with multibyte strings, which may contain characters with varying widths, it becomes more complex. This question delves into the nuances of truncating multibyte strings while preserving word boundaries.

PHP's mb_strimwidth Function

As suggested by the answer, PHP provides a convenient function called mb_strimwidth() that handles truncation of multibyte strings. It takes the string, desired width, and an optional terminator as arguments. However, this function does not take into account word boundaries.

Custom Truncation Algorithm

To achieve word boundary truncation, a custom algorithm can be used:

function truncate($string, $chars = 50, $terminator = ' …') {
    // Calculate the maximum length considering the terminator
    $max_length = $chars - mb_strlen($terminator);

    // Short circuit for strings shorter than the maximum length
    if (mb_strlen($string) <= $max_length) {
        return $string;
    }

    // Find the last space character within the maximum length
    $last_space_index = mb_strrpos($string, ' ', $max_length);

    // Cut the string at the last space or at the maximum length if no last space is found
    $truncated_string = (false !== $last_space_index)
        ? mb_substr($string, 0, $last_space_index)
        : mb_strimwidth($string, 0, $chars);

    // Append the terminator
    $truncated_string .= $terminator;

    return $truncated_string;
}
Copy after login

This function implements the steps described in the question:

  1. Calculate the maximum length.
  2. Validate if the string is longer than the maximum length.
  3. Find the last space character below the maximum length.
  4. Cut the string accordingly.
  5. Append the terminator.
  6. Return the truncated string.

Demonstration

The following code demonstrates the usage of the custom truncation function:

$in = "Answer to the Ultimate Question of Life, the Universe, and Everything.";
$out = truncate($in, 50, ' …');

echo $out; // "Answer to the Ultimate Question of Life, the …"
Copy after login

Conclusion

PHP's mb_strimwidth() function provides a straightforward way to truncate multibyte strings, but it does not consider word boundaries. By implementing a custom algorithm, we can achieve more precise truncation that preserves the integrity of word boundaries.

The above is the detailed content of How Can I Precisely Truncate Multibyte Strings While Maintaining Word Boundaries?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template