PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Sep 21, 2018 6:39 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: str_split for PHP < 5
PostPosted: Fri Mar 24, 2006 12:40 am 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 5001
Location: Indiana
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.

Syntax: [ Download ] [ Hide ]
<?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
Syntax: [ Download ] [ Hide ]
<?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
)
*/

 
?>

_________________
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.


Last edited by feyd on Fri Aug 01, 2008 9:58 am, edited 5 times in total.
Fix php tags.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 12:55 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
to help so you can get rid of the @ and use the proper name:
Syntax: [ Download ] [ Hide ]
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.
fix php tags.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 1:03 am 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 5001
Location: Indiana
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.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 8:28 am 
Offline
Forum Newbie
User avatar

Joined: Fri Jun 14, 2002 1:48 pm
Posts: 16
Hi,

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

A slightly shorter version might be:

Syntax: [ Download ] [ Hide ]
<?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.
fix php tags.


Top
 Profile  
 
PostPosted: Wed Jul 16, 2008 12:04 pm 
Offline
Forum Newbie

Joined: Wed Jul 16, 2008 11:57 am
Posts: 1
Okay, here is another way you could get this

Syntax: [ Download ] [ Hide ]
 
<?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.
fix php tags and remove ad


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group