Jump to: navigation, search

Php str split

From w3cyberlearnings

Contents

PHP function str_split

This function converts a given string into array.

Syntax str_split

  • string: string input.
  • width: the width of each array character. Default value is 1
str_split(string, width);

Example 1

<?php

$str='w3Cyberlearnings.com';
$aa_str = str_split($str);

print_r($aa_str);
?>

Output

Array
(
    [0] => w
    [1] => 3
    [2] => C
    [3] => y
    [4] => b
    [5] => e
    [6] => r
    [7] => l
    [8] => e
    [9] => a
    [10] => r
    [11] => n
    [12] => i
    [13] => n
    [14] => g
    [15] => s
    [16] => .
    [17] => c
    [18] => o
    [19] => m
)

Example 2

  • Split array into 3 characters length.
<?php

$str='w3Cyberlearnings.com';
$aa_str = str_split($str,3);

print_r($aa_str);
?>

Output

Array
(
    [0] => w3C
    [1] => ybe
    [2] => rle
    [3] => arn
    [4] => ing
    [5] => s.c
    [6] => om
)

Related Functions


Navigation
Web
SQL
MISC
References