When you need to split a string into fixed-size byte chunks, it helps to understand both the normal result and what changes with different inputs. This guide focuses on practical examples rather than repeating PHP background material.
str_split provides the behavior described below. The last chunk may be shorter than the requested size. UTF-8 characters may be split in the middle.
What Is str_split in PHP?
Split a string into fixed-size byte chunks. The last chunk may be shorter than the requested size. UTF-8 characters may be split in the middle.
Its syntax is:
str_split(string $string, int $length = ...): array
Parameters and return value: Read the argument names and optional values in the signature above. Methods use an existing object, while stand-alone functions take direct arguments. The examples demonstrate the relevant arguments and the value returned or the object modified.
Important: Chunk length must be positive. Use mb_str_split() for multibyte character splitting.
Example 1: Basic Usage
This first example demonstrates how to split a string into fixed-size byte chunks with a small input.
<?php
print_r(str_split("ABC123", 2));
Output:
Array
(
[0] => AB
[1] => C1
[2] => 23
)
The example uses the simplest useful form. The last chunk may be shorter than the requested size. UTF-8 characters may be split in the middle. The output may depend on the active ICU locale, PHP configuration, or input supplied to the function.
Example 2: Another Case
Here the input or option changes, making it easier to see where the behavior differs. Chunk length must be positive. Use mb_str_split() for multibyte character splitting.
<?php
print_r(str_split("red", 1));
Output:
Array
(
[0] => r
[1] => e
[2] => d
)
Compare the result to the first example. explode() splits on delimiters; preg_split() splits using a regex.
Practical Example: Applying the Function
This standalone example shows how the same operation fits into a small application task.
<?php
$serialCode = "AA55BB77";
$segments = str_split($serialCode, 2);
echo implode("-", $segments);
Output:
AA-55-BB-77
The example keeps the operation visible and gives you a starting point for using it in an application. Chunk length must be positive. Use mb_str_split() for multibyte character splitting.
Important Behavior and Edge Cases
- Core behavior: The last chunk may be shorter than the requested size. UTF-8 characters may be split in the middle.
- Return value: Check the result separately from changes to passed-in arrays, iterators, or object state.
- Data constraints: Chunk length must be positive. Use mb_str_split() for multibyte character splitting.
- Environment: Run this example under a PHP version supporting the API; check results and exceptions before using them.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
str_split | Split a string into fixed-size byte chunks. |
| Related PHP APIs | explode() splits on delimiters; preg_split() splits using a regex. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming the function works on every kind of input | Follow the declared argument types and check the input format before calling it. |
| Ignoring the behavior of the result | Chunk length must be positive. Use mb_str_split() for multibyte character splitting. |
| Using the wrong related PHP API | explode() splits on delimiters; preg_split() splits using a regex. |
Key Takeaways
str_splithelps you split a string into fixed-size byte chunks.- The last chunk may be shorter than the requested size. UTF-8 characters may be split in the middle.
- Chunk length must be positive. Use mb_str_split() for multibyte character splitting.
- explode() splits on delimiters; preg_split() splits using a regex.
str_split is useful when you need to split a string into fixed-size byte chunks. Start with the small examples, then adapt the practical example while respecting its input and environment requirements.