PHP str_split Function Guide
PHP str_split Function Guide
str_split
Description ¶
Parameters ¶
string
The input string.
length
Maximum length of the chunk.
Return Values ¶
Examples ¶
<?php
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
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
)
Notes ¶
Note:
str_split() will split into bytes, rather than
characters when dealing with a multi-byte
encoded string. Use mb_str_split() to split the
string into code points.
See Also ¶
<?php
function str_split_unicode($str, $l = 0)
{
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i < $len; $i +=
$l) {
$ret[] = mb_substr($str, $i,
$l, "UTF-8");
}
return $ret;
}
return preg_split("//u", $str, -1,
PREG_SPLIT_NO_EMPTY);
}
?>
print_r(str_split($s, 3));
print_r(str_split_unicode($s, 3));
Array
(
[0] => Il�
[1] => �k
[2] => sü
[3] => t
)
Array
(
[0] => Ilı
[1] => k s
[2] => üt
)
<?php
function str_split_unicode($str, $length
= 1) {
$tmp = preg_split('~~u', $str, -1,
PREG_SPLIT_NO_EMPTY);
if ($length > 1) {
$chunks = array_chunk($tmp,
$length);
foreach ($chunks as $i =>
$chunk) {
$chunks[$i] = join('',
(array) $chunk);
}
$tmp = $chunks;
}
return $tmp;
}
?>
print_r(str_split_unicode($s));
print_r(str_split_unicode($s, 3));
Array
(
[0] => Ö
[1] => z
[2] => g
[3] => ü
[4] => r
[5] =>
[6] => Y
[7] => a
[8] => z
[9] => ı
[10] => l
[11] => ı
[12] => m
[13] => !
)
Array
(
[0] => Özg
[1] => ür
[2] => Yaz
[3] => ılı
[4] => m!
)
if(! function_exists('str_split'))
{
function str_split($text, $split
= 1)
{
$array = array();
return $array;
}
}
<?php
?>
function str_split($str){
return preg_split('//',$str);
}
<?php
<?php
$spl1 = str_split($str1);
$spl2 = str_split($str2);
$spl3 = str_split($str3);
$spl4 = str_split($str4);
$spl5 = str_split($str5);
$spl6 = str_split($str6);
$spl7 = str_split($str7);
echo count($spl1); // 4
echo count($spl2); // 1
echo count($spl3); // 1
echo count($spl4); // 2
echo count($spl5); // 3
echo count($spl6); // 1
echo count($spl7); // 1
print_r($spl1);
print_r($spl2);
print_r($spl3);
print_r($spl4);
print_r($spl5);
print_r($spl6);
print_r($spl7);
/*
Array
(
[0] => L
[1] => o
[2] => n
[3] => g
)
Array
(
[0] => x
)
Array
(
[0] =>
)
Array
(
[0] => 3
[1] => 4
)
Array
(
[0] => 3
[1] => .
[2] => 4
)
Array
(
[0] => 1
)
Array
(
[0] =>
)
*/
?>
if(strlen($string)>$string_length ||
!$string_length) {
do {
$c = strlen($string);
$parts[] =
substr($string,0,$string_length);
$string =
substr($string,$string_length);
} while($string !== false);
} else {
$parts = array($string);
}
return $parts;
}
}
?>
<code>
function str_split($text, $split = 1){
//place each character of the string
into and array
$array = array();
for ($i=0; $i < strlen($text); $i++)
{
$key = "";
for ($j = 0; $j < $split; $j++){
$key .= $text[$i+$j];
}
$i = $i + $j - 1;
array_push($array, $key);
}
return $array;
}
</code>
<?php
function uni_strsplit($string,
$split_length=1)
{
preg_match_all('`.`u', $string,
$arr);
$arr = array_chunk($arr[0],
$split_length);
$arr = array_map('implode', $arr);
return $arr;
}
<?php
$len = strlen($text);
$array = array();
$i = 0;
$i += 1;
}
$array[] = $key;
}
return $array;
}
?>
<?php
function str_rsplit($str, $sz)
{
// splits a string "starting" at the
end, so any left over (small chunk) is
at the beginning of the array.
if ( !$sz ) { return false; }
if ( $sz > 0 ) { return
str_split($str,$sz); } // normal
split
$l = strlen($str);
$sz = min(-$sz,$l);
$mod = $l % $sz;
if ( !$mod ) { return
str_split($str,$sz); } // even/max-
length split
// split
return
array_merge(array(substr($str,0,$mod)),
str_split(substr($str,$mod),$sz));
}
$str = 'aAbBcCdDeEfFg';
str_split($str,5); // return:
{'aAbBc','CdDeE','fFg'}
str_rsplit($str,5); // return:
{'aAbBc','CdDeE','fFg'}
str_rsplit($str,-5); // return:
{'aAb','BcCdD','eEfFg'}
?>
<?php
if(!function_exists('str_split')) {
function str_split($string,
$split_length = 1) {
$array = explode("\r\n",
chunk_split($string, $split_length));
array_pop($array);
return $array;
}
}
?>
if ( !function_exists('str_split') ) {
function
str_split($string,$split_length=1){
$sign = (($split_length<0)?-1:1);
$strlen = strlen($string);
$split_length = abs($split_length);
if ( ($split_length==0) ||
($strlen==0) ){
$result = false;
//$result[] = "";
}
elseif ($split_length >= $strlen){
$result[] = $string;
}
else {
$length = $split_length;
for ($i=0; $i<$strlen; $i++){
$i=(($sign<0)?
$i+$length:$i);
$result[] =
substr($string,$sign*$i,$length);
$i--;
$i=(($sign<0)?
$i:$i+$length);
if ( ($i+$split_length) >
($strlen) ){
$length = $strlen-
($i+1);
}
else {
$length = $split_length;
}
}
}
return $result;
}
}
<?php
print_r(str_split(0080450)); // does
not work
print_r(str_split(strval(0080450))); //
neither this
/*
* Outputs:
* Array
* (
* [0] => 0
* )
*/
?>
BUT
<?php
print_r(str_split(80450)); // works
fine
print_r(str_split(strval(80450))); //
so does this
/*
* Outputs:
* (
* [0] => 8
* [1] => 0
* [2] => 4
* [3] => 5
* [4] => 0
* )
*/
?>
/*
Outputs:
* Array
* (
* [0] => 8
* [1] => 0
* [2] => 4
* [3] => 5
* [4] => 0
* [5] => .
* [6] => 0
* [7] => 0
* [8] => 1
* )
*/
?>
<?php
if (!function_exists('mb_str_split')) {
/**
* Converts an UTF-8 string to an
array.
*
* E.g. mb_str_split("Hello
Friend");
* returns ['H', 'e', 'l', 'l', 'o',
' ', 'w', 'o', 'r', 'l', 'd']
*
* @param string $string The input
string.
* @param int $split_length Maximum
length of the chunk. If specified, the
returned array will be broken down
* into chunks with each
being split_length in length, otherwise
each chunk will be one character in
length.
* @return array|boolean
* -
* - If the split_length
length exceeds the length of string, the
entire string is returned
* as the first (and only)
array element.
* - False is returned if
split_length is less than 1.
*/
function mb_str_split($string,
$split_length = 1)
{
if ($split_length == 1) {
return preg_split("//u",
$string, -1, PREG_SPLIT_NO_EMPTY);
} elseif ($split_length > 1) {
$return_value = [];
$string_length =
mb_strlen($string, "UTF-8");
for ($i = 0; $i <
$string_length; $i += $split_length) {
$return_value[] =
mb_substr($string, $i, $split_length,
"UTF-8");
}
return $return_value;
} else {
return false;
}
}
}
?>
<?php
if (!function_exists("str_split")) {
function str_split($string, $length
= 1) {
if ($length <= 0) {
trigger_error(__FUNCTION__."
(): The the length of each segment must
be greater then zero:", E_USER_WARNING);
return false;
}
$splitted = array();
$str_length = strlen($string);
$i = 0;
if ($length == 1) {
while ($str_length--) {
$splitted[$i] =
$string[$i++];
}
} else {
$j = $i;
while ($str_length > 0) {
$splitted[$j++] =
substr($string, $i, $length);
$str_length -= $length;
$i += $length;
}
}
return $splitted;
}
}
?>
<?php
/**
Returns a formatted string
based on camel case.
e.g. "CamelCase" -> "Camel
Case".
*/
function FormatCamelCase(
$string ) {
$output = "";
foreach( str_split(
$string ) as $char ) {
strtoupper(
$char ) == $char and $output and $output
.= " ";
$output .=
$char;
}
return $output;
}
?>
$array
[0] -> false
[1] -> true
[2] -> true
[3] -> false
[4] -> false
[5] -> true
[6] -> true
[7] -> false
[8] -> false
[9] -> false
[10] -> true
[11] -> false
[12] -> false
[13] -> true
[14] -> false
[15] -> false
...
[23] -> true
<?php
function enum_to_array($psEnum)
{
$aReturn = array();
$aTemp = explode(', ', $psEnum);
for ($i = $aTemp[0]; $i <=
$aTemp[count($aTemp)-1]; $i++)
{
$aReturn[$i] = in_array($i,
$aTemp);
}
}
?>
<?php
function str_split_php4($text, $split =
1){
//place each character of the string
into and array
$array = array();
for($i=0; $i < strlen($text); $i++){
$key = NULL;
for ($j = 0; $j < $split; $j++){
$key .= $text[$i];
}
array_push($array, $key);
}
return $array;
}
?>
<?php
if (phpversion () < "5"){ // define PHP5
functions if server uses PHP4
<?php
preg_split('#(?<=.)(?=.)#s', $str);
?>
function string_split($str)
{
$str_array=array();
$len=strlen($str);
for($i=0;$i<$len;$i++)
$str_array[]=$str{$i};
return $str_array;
}
//example :
var_dump (string_split("split this"));
?>
<?php
if (!function_exists("str_split")) {
function str_split($str,$length = 1) {
if ($length < 1) return false;
$strlen = strlen($str);
$ret = array();
for ($i = 0; $i < $strlen; $i +=
$length) {
$ret[] = substr($str,$i,$length);
}
return $ret;
}
}
?>
<?php
}
print cloakEmail('[email protected]');
?>
<?php
function cloakEmail($email) {
$arChars = array();
for ($i = 0; $i < strlen($email);
$i++) { $arChars[] = $email[$i]; }
foreach ($arChars as $char) { print
'&#'.ord($char); }
}
print cloakEmail('[email protected]');
?>
<?php
function str_split_php4_utf8($str) {
// place each character of the
string into and array
$split=1;
$array = array();
for ( $i=0; $i < strlen( $str ); ){
$value = ord($str[$i]);
if($value > 127){
if($value >= 192 && $value
<= 223)
$split=2;
elseif($value >= 224 &&
$value <= 239)
$split=3;
elseif($value >= 240 &&
$value <= 247)
$split=4;
}else{
$split=1;
}
$key = NULL;
for ( $j = 0; $j < $split; $j++,
$i++ ) {
$key .= $str[$i];
}
array_push( $array, $key );
}
return $array;
}
?>
<?php
/* reverse translate an aa sequence
using its dna counterpart */
function reverseTranslate($aaSeq,$ntSeq)
{
$nt=str_split($ntSeq,3);
$aa=str_split($aaSeq,1);
$gapChar=array('*','-');
$numAa=count($aa);
$ntIndex=0;
$newNtSeq="";
for($i=0;$i<$numAa;$i++){
// if the aa is a gap, then just put
on a gap character
if(in_array($aa[$i],$gapChar)){
$newNtSeq.='---';
}
else{
$newNtSeq.=$nt[$ntIndex];
$ntIndex++;
}
}
return $newNtSeq;
}
?>
<?php
$test =
'CustomerIDWithSomeOtherJETWords';
preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-
z])|[A-Z][a-z]/', ' $0', $test));
?>
<?php
/**
* Split a string into two parts at
offset.
*
* @param string $string
* @param integer $offset
* @return mixed array and
bool(false) if offset is out of scope
*/
/**
* Output:
array(2) {
[0]=>
string(12) "Split a stri"
[1]=>
string(27) "ng into two parts at
offset"
}
*/
?>
<?php
if(!function_exists('str_split')){
function
str_split($string,$split_length=1){
$count = strlen($string);
if($split_length < 1){
return false;
} elseif($split_length > $count)
{
return array($string);
} else {
$num =
(int)ceil($count/$split_length);
$ret = array();
for($i=0;$i<$num;$i++){
$ret[] =
substr($string,$i*$split_length,$split_l
ength);
}
return $ret;
}
}
}
?>
add a note
String Functions
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hex2bin
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
lcfirst
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quoted_printable_encode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_contains
str_ends_with
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
» str_split
str_starts_with
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
utf8_decode
utf8_encode
vfprintf
vprintf
vsprintf
wordwrap
Deprecated
convert_cyr_string
hebrevc