str_split for PHP < 5

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

str_split for PHP < 5

Post by s.dot »

I'm running php 4.4.1 and needed to use str_split so I came up with this function. Thought I'd share it with you guys.

Code: Select all

<?php
 
//  array str_split ( string string [, int split_length] )
 
if(!function_exists('str_split')){
    
    function str_split($string,$split_length=1){
    
        $count = strlen($string);
    
        if($split_length < 1){
        
            //  return false if split length is less than 1
            //  to mimic php 5 behavior
            return false;
        
        } elseif($split_length > $count){
        
            //  the entire string becomes a single element
            //  in an array
            return array($string);
        
        } else {
        
            //  split the string at desired length
            $num = (int)ceil($count/$split_length);
            $ret = array();
            for($i=0;$i<$num;$i++){
                $ret[] = substr($string,$i*$split_length,$split_length);
            }
            return $ret;
            
        }   
    }
}
?>
Usage

Code: Select all

<?php
 
// with split length specified
echo str_split('abcdefghijklmnopq',2);
 
/*
Array
(
    [0] => ab
    [1] => cd
    [2] => ef
    [3] => gh
    [4] => ij
    [5] => kl
    [6] => mn
    [7] => op
    [8] => q
)
*/
 
// without split length specified
echo str_split('abcdefghijklmnopq');
 
/*
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
    [9] => j
    [10] => k
    [11] => l
    [12] => m
    [13] => n
    [14] => o
    [15] => p
    [16] => q
)
*/
 
?>
Last edited by feyd on Fri Aug 01, 2008 9:58 am, edited 5 times in total.
Reason: Fix php tags.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

to help so you can get rid of the @ and use the proper name:

Code: Select all

if(!function_exists('str_split'))
{
  function str_split($string, $split_length = 1) {
  }
}
Last edited by feyd on Fri Aug 01, 2008 9:59 am, edited 1 time in total.
Reason: fix php tags.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

thanks, i reflected the changes in the snippet.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
amnuts
Forum Newbie
Posts: 16
Joined: Fri Jun 14, 2002 1:48 pm
Contact:

Post by amnuts »

Hi,

I know this was posted a while ago, but still...

A slightly shorter version might be:

Code: Select all

<?php
 
if (!function_exists('str_split')) {
    function str_split($string, $split_length = 1) {
        if ($split_length < 1) {
            return false;
        }
        return array_filter(explode('[-^%^-]', chunk_split($string, $split_length, '[-^%^-]')));
    }
}
 
 
print_r(str_split('abcdefghijklmnopq'));
print_r(str_split('abcdefghijklmnopq', 2));
 
?>
Regards,

Andy
Last edited by feyd on Fri Aug 01, 2008 9:59 am, edited 1 time in total.
Reason: fix php tags.
blackid
Forum Newbie
Posts: 1
Joined: Wed Jul 16, 2008 11:57 am

Re: str_split for PHP < 5

Post by blackid »

Okay, here is another way you could get this

Code: Select all

 
<?php
function str_split($str)
    {
        for($i=0;$i<strlen($str);$i++)
            {
                $ret[$i] = substr($str,$i,1);
            }
        return $i;
    }
 
?>
 
Ofcourse you can add this and other things that you have written to mimic PHP 5 behaviour. But this is again an option. I have found the for loop quite interesting in my programming career ;)

Hope this is helpful.

Thanks

Jyot Vakharia
Last edited by feyd on Fri Aug 01, 2008 10:00 am, edited 2 times in total.
Reason: fix php tags and remove ad
Post Reply