array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'es', ), 'this' => array ( 0 => 'function.str-split.php', 1 => 'str_split', ), 'up' => array ( 0 => 'ref.strings.php', 1 => 'Funciones de strings', ), 'prev' => array ( 0 => 'function.str-shuffle.php', 1 => 'str_shuffle', ), 'next' => array ( 0 => 'function.str-starts-with.php', 1 => 'str_starts_with', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'es', 'path' => 'reference/strings/functions/str-split.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5, PHP 7, PHP 8)
str_split — Convierte un string en un array
Convierte un string en un array.
string
El string de entrada.
split_length
Longitud máxima del fragmento.
Si el parámetro opcional split_length
se
especifica, el array devuelto será separado en fragmentos los cuales cada uno
tendrá una longitud de split_length
, de otra manera cada fragmento
tendrá una longitud de un caracter.
Se devuelve false
si split_length
es menor que 1.
Si la longitud split_length
excede la longitud de
string
, el string entero es devuelto como el primero
(y único) elemento del array.
Ejemplo #1 Ejemplos de uso de str_split()
<?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
El resultado del ejemplo sería:
Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => F [7] => r [8] => i [9] => e [10] => n [11] => d ) Array ( [0] => Hel [1] => lo [2] => Fri [3] => end )
Nota:
str_split() realizará la división en bytes en lugar de en caracteres cuando trata con un string codificado en multibyte.