+4 votes
in Programming Languages by (56.8k 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

+1 vote
by (349k points)
selected by
 
Best answer

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
)


...