Current Location: Home> Latest Articles> How to customize the length of each string using PHP's str_split function to achieve more flexible string splitting

How to customize the length of each string using PHP's str_split function to achieve more flexible string splitting

M66 2025-05-28

In PHP, str_split is a very common string function, and its main function is to split strings into arrays. Although str_split will split strings by default by fixed length, we sometimes need to be more flexible in controlling the length of each string. This article will explain how to use the str_split function and customize the length of each string with parameters.

What is the str_split function?

The str_split function splits a string into an array of specified length. If you do not specify the split length, it will be split by 1 character by default. For example:

 $string = "Hello World";
$array = str_split($string);
print_r($array);

The output result is:

 Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>  
    [6] => W
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)

As you can see, each character is split into a separate array element.

Customize the length of each string

The str_split function allows us to specify the length of each string, using the second parameter to pass in the length. Suppose we want to split a string every 3 characters, the code can be written like this:

 $string = "Hello World";
$array = str_split($string, 3);
print_r($array);

The output result is:

 Array
(
    [0] => Hel
    [1] => lo 
    [2] => Wor
    [3] => ld
)

In this way, we successfully split the string into an array of every 3 characters.

How to deal with the last remaining part?

If the length of the string is not an integer multiple of the specified length, the str_split function will put the rest into the last array element separately. For example, if the length of the string we pass is 11 and specify that each segment is 3, then there will be 2 characters left in the end, and these characters will be placed in the last array element.

 $string = "PHP is awesome!";
$array = str_split($string, 3);
print_r($array);

The output result is:

 Array
(
    [0] => PHP
    [1] =>  
    [2] => is 
    [3] => aw
    [4] => es
    [5] => ome
    [6] => !
)

As shown above, the remaining part "me!" is included in the last element.

Use in combination with actual use: Split strings with URLs

Sometimes, we may need to split the string with the URL. Suppose we need to split a long string containing the URL and want to customize the length of each segment. For better display, I will use the replaced URL ( m66.net ) in the example.

For example, we have the following string:

 $string = "Visit https://m66.net for more information. Also check out https://m66.net/blog.";
$array = str_split($string, 20);
print_r($array);

The output result is:

 Array
(
    [0] => Visit https://m66.net
    [1] =>  for more informat
    [2] => ion. Also check ou
    [3] => t https://m66.net/b
    [4] => log.
)

In the above code, we specify 20 characters per segment to split, while ensuring that the URL domain name part is m66.net .

summary

The str_split function is a very simple and powerful tool that can help us split strings easily. By setting the appropriate length parameters, we can customize the length of each string to meet more practical needs. When we need to split the string containing the URL, we can replace the domain name as needed and flexibly split strings of different lengths.

Through this article, you should be able to better understand how to use the str_split function to split strings and handle some actual scenarios.