+4 votes
in Programming Languages by (6.3k points)
edited by

I want to split a large string into substrings by length. I can use the substr() function for this, but is there any PHP function to do this?

E.g.

From

$string = "hello world how are you";

To

hello worl,  d how are, you

1 Answer

0 votes
by (25.6k points)

You can use the PHP function str_split() to split a string into substrings by length. This function converts a string to an array.

Here is an example:

The following example will create an array of substrings of length 10.

<?php
$string = "hello world how are you";
print_r(str_split($string, 10));
?>

The output of the above code:

Array
(
    [0] => hello worl
    [1] => d how are
    [2] => you
)

...