str_split() : splits a string into an array of smaller chunks

$string="Welcome to Plus2net Php section";
print_r(str_split($string,5));
The output is here
Array ( [0] => Welco [1] => me to [2] => Plus [3] => 2net [4] => Php s [5] => ectio [6] => n ) 
We can split a string with fixed intervals ( without using any delimiter ) by using PHP str_split command. The output we will get an array with broken string of fixed length. This breaking of string is different than explode as no delimiter is used to split the string.

Here is the syntax.
str_split(string, integer of length )
Let us see if we don't use any length integer. The output array will have elements of single char. Means it will be broken across each char or the default value will be 1.
$string="Welcome to Plus2net Php section";
print_r(str_split($string));
Output is here
Array ( [0] => W [1] => e [2] => l [3] => c [4] => o [5] => m [6] => e [7] => [8] => t
 [9] => o [10] => [11] => P [12] => l [13] => u [14] => s [15] => 2 [16] => n [17] => e 
[18] => t [19] => [20] => P [21] => h [22] => p [23] => [24] => s [25] => e [26] => c 
[27] => t [28] => i [29] => o [30] => n ) 

Example 2: Splitting Strings with Non-English Characters

The *str_split()* function can handle non-English characters, but care is needed with multibyte strings (e.g., UTF-8). For such cases, *mb_str_split()* is recommended:

$string = "Добро пожаловать";
print_r(mb_str_split($string, 4)); 
// Outputs: Array ( [0] => Добр [1] => о по [2] => жал [3] => оват )

Example 3: Splitting Strings into a Custom Length

Here's an example that splits the string into custom-length substrings:

$string = "123456789";
print_r(str_split($string, 3)); 
// Outputs: Array ( [0] => 123 [1] => 456 [2] => 789 )

Use Case: Chunking Strings for Data Formatting

You can use *str_split()* to format long strings, such as splitting a string into smaller segments for display:

$long_string = "ThisIsAVeryLongStringWithoutSpaces";
$chunks = str_split($long_string, 5);
foreach ($chunks as $chunk) {
    echo $chunk . "<br>";
}
// Outputs the string in 5-character chunks
Output
ThisI
sAVer
yLong
Strin
gWith
outSp
aces

Example 4: Handling Empty Strings

If you pass an empty string to str_split(), the function returns an empty array:

$empty_string = "";
print_r(str_split($empty_string)); 
// Outputs: Array ( )

Example 5: Combining str_split() with implode() for Reformatting

You can combine str_split() with implode() to reformat strings by adding separators between chunks:

$card_number = "1234567812345678";
$formatted = implode("-", str_split($card_number, 4));
echo $formatted; 
// Outputs: 1234-5678-1234-5678

String Functions nl2br() To add Line break
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com











    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer