#development #pattern #php

In the world of web development, there are many scenarios where you may need to break a long piece of text into smaller, more manageable chunks. Whether you're working on a content management system, an email processing script, or a text formatting tool, efficiently splitting text is a common task. In this blog post, we'll explore a PHP function that can help you achieve this efficiently.

The Challenge

Imagine you have a lengthy block of text, such as a news article, a user-generated comment, or even a large string of data. You want to split this text into smaller chunks, ensuring that each chunk doesn't exceed a specified character limit. Moreover, you want to maintain the integrity of words and avoid splitting them in the middle. This can be particularly challenging when dealing with HTML content or text that contains line breaks and excessive whitespace.

The Solution

To tackle this problem, we can create a PHP function called splitTextIntoChunks. This function takes two parameters: the input text and the maximum chunk size. It then processes the text and returns an array of text chunks, ensuring that no word is broken in the middle. Let's break down the code step by step.

 1function splitTextIntoChunks(string $text, int $maxChunkSize): array
 3    // Step 1: Replace line breaks and excessive whitespace with single spaces.
 4    $text = str_replace(["\r\n", "\r", "\n"], ' ', $text);
 5    $text = preg_replace('/\s+/', ' ', $text);
 7    // Step 2: Trim the text to remove leading and trailing spaces.
 8    $text = trim($text);
10    // Step 3: If the text is empty, return an empty array.
11    if ($text === '') {
12        return [];
13    }
15    // Step 4: Use wordwrap to split the text into chunks.
16    return explode("\n", wordwrap($text, $maxChunkSize, "\n", false));

Let's break down the key steps of this function:

  1. Cleaning the Text: The function first removes line breaks and replaces consecutive whitespace characters with a single space, ensuring uniform spacing within the text.

  2. Trimming the Text: Any leading or trailing spaces are trimmed from the text to prevent unwanted spaces in the resulting chunks.

  3. Checking for Empty Text: If the input text becomes empty after cleaning and trimming, the function returns an empty array, indicating that there are no chunks to split.

  4. Chunking the Text: Finally, the function utilizes wordwrap to split the text into chunks of a maximum size specified by $maxChunkSize. The use of \n as the line break character ensures that words are not split in the middle. The result is an array of text chunks, each separated by a newline character.

Example Usage

Here's an example of how you can use the splitTextIntoChunks function:

1$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nonummy tincidunt ut lacreet dolore magna aliquam erat volutpat.";
3$chunks = splitTextIntoChunks($text, 30);
5foreach ($chunks as $chunk) {
6    echo $chunk . "\n";

In this example, the function will split the text into chunks, each containing a maximum of 30 characters (adjustable to your needs).


Efficiently splitting text into manageable chunks is a common requirement in web development, and the splitTextIntoChunks function in PHP provides an elegant solution. By cleaning and trimming the text while ensuring words remain intact, you can effectively break down large text blocks for various applications, such as content management systems, email processing, and more. Feel free to incorporate and adapt this function to suit your specific needs, making text processing tasks in PHP a breeze.