SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but I can not do it... agghhhh

    how do I take a string like...."01110011001101000010011000110101100100110100011000110110110100110111011001001011111101000110011"

    and explode to an array, each element consisting of 4 chars? ($arr[0]='0111';$arr[1]='0011'; etc)

    I know there is a simple way (PHP3 only for this one) - but I am pulling my hair out cos I can not find it.

    I have got a work around but its ugly and probably totally unn'y.

    Help much appreciated .

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code...

    PHP Code:
    $num 4;
    $str "0000111100001111";
    $length strlen($str);
    $total $length $num;

      for (
    $i 0$i <= $total$i++) { 
        
    $array[$i] = substr($str, ($i $num), $num);
      }

      for (
    $x 0$x <= count($array); $x++) {
      echo(
    "$array[$x]<br>");
      } 
    Produces this output:

    Code:
    0000
    1111
    0000
    1111
    Adding any other characters onto the end shouldn't be a problem - it'll just tack them on into the last element of the array.

    Man, that was tougher than I thought!

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Slightly different approach for your use

    PHP Code:
    $str "01110011001101000010011000110101100100110100011000110110110100110111011001001011111101000110011";
    while(
    $i<strlen($str)) {
        
    $array[] = substr($str$i4);
        
    $i $i 4;
        }
    foreach(
    $array as $val) {
        print 
    $val."<br>";
        } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dangitt, Freddy out-does me again. Only change I would make is to assign the number "4" to a variable at the top of things to more easily modify it.

  5. #5
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Folks, thanks for the replies, I did this

    for($p=0;$p<=strlen($serial);$p++){
    $serial1.=$serial[$p];$q++;if($q>3){
    $serial1.=";";$q=0;}
    }

    and then explode()'d by ";".. very messy! - both of your suggestions are cleaner!

    I tried spit and ereg but could not get anything to work,

    As it happens... I now do not need to break the string into blocks of 4 never mind only a couple of hours down the tube! -

    Thanks again.

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried something similar - I tried to use a regular expression and the split() function, but failed miserably, as is becoming the custom with me when dealing with regular expressions.

  7. #7
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here's the regexp version...

    PHP Code:
    $thestr "10101010010101010110110101011101001010";
    while (
    ereg("^(.{1,4})(.*)$",$thestr,$matches)) {
      
    $thearray[] = $matches[1];
      
    $thestr $matches[2];

    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  8. #8
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kyank - thats ****** marvelous ! -

    Beacause now I find I have to split the string into blocks of 2 bits, so I can just modify that code .

    Thanks again all for the help.

  9. #9
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Glad to be of service.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •