Explode at Nothing?

Discussion in 'PHP' started by Pudge1, Aug 20, 2009.

  1. #1
    Is there any function or possible way you could do the equivilent of this:
    
    explode('',$array)
    
    Code (markup):
    ?
     
    Pudge1, Aug 20, 2009 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    Please be clear when you ask your question

    Do u know what explode do?

    $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
    $pieces = explode(" ", $pizza);
    echo $pieces[0]; // piece1
    echo $pieces[1]; // piece2

    This pizza is a String not array
    and pieces is array.

    Please tell us the content of string which you want to convert into array
    Regards

    Alex
     
    Last edited: Aug 20, 2009
    kmap, Aug 20, 2009 IP
  3. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    It doesn't matter. Like lets say

    textfile.txt
    
    123456
    
    Code (markup):
    phpfile.php
    
    $file = file_get_contents('textfile.txt');
    $array = explode('',$file);
    
    Code (markup):
    So it would be like this
    $array[0] = 1
    $array[1] = 2
    $array[2] = 3
    ...

    Do you get what I am trying to do?
     
    Pudge1, Aug 20, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Actually if it's a string you can already do this:

    $string[0] etc.. Without converting to an array. Ofcourse, foreach probably wouldn't work. Well, I don't know, haven't tried.
     
    premiumscripts, Aug 20, 2009 IP
  5. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    So if like $string = hotdog then $string[0] = h $string[1] = o...?
     
    Pudge1, Aug 20, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, just test it in a new php file.

    $string = 'hotdog'; echo $string[0]; ?>
    PHP:
     
    premiumscripts, Aug 20, 2009 IP
  7. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #7
    Hmm I added it to my script now I get a 500 Internal Server Error haha. Anyway want to help?

    
    <?
    function Mid($str,$start,$length)
    {
    $array = $str;
    $number = $start;
    while($length > $number)
    {
    $return .= $array[$number];
    $number++;
    }
    return $return;
    }
    
    function CLng($str)
    {
    $array = explode('.',$str);
    return $array[0];
    }
    
    function Decompress($DecompData)
    {
        if (strlen($DecompData) > 4)
        {
            $ControlCount = ord(substr($DecompData,0,1)) *16777216 + ord(Mid($DecompData, 2, 1)) *65536 + ord(Mid($DecompData, 3, 1)) *256 + ord(Mid($DecompData, 4, 1));
            $DataCount = ord(Mid($DecompData, 5, 1)) *16777216 + ord(Mid($DecompData, 6, 1)) *65536 + ord(Mid($DecompData, 7, 1)) *256 + ord(Mid($DecompData, 8, 1)) + 9;
            $Decompress = Mid($DecompData, $DataCount, 4);
            $DataCount = $DataCount + 4;
            $ControlBitPos = 0;
            $ControlPos = 9;
        }
        else
        {
        }
    
    
            $DataPost = 1;
    
            while($DataPos < $ControlCount)
            {
                if(pow(2, $ControlBitPos) == (ord(substr($DecompData, $ControlPos, 1))) && pow(2, $ControlBitPos))
                {
                    $DecompStart = strlen($Decompress) - (CLng(ord(Mid($DecompData, $DataCount, 1))) *256 + CLng(ord(Mid($DecompData, $DataCount + 1, 1)))) + 1;
                    $DecompLen = ord(Mid($DecompData, $DataCount + 2, 1));
                    $Decompress = $Decompress & Mid($Decompress, $DecompStart, $DecompLen);
                    $DataCount = $DataCount + 3;
                   }
                else
                {
                    $Decompress = $Decompress . Mid($DecompData, $DataCount, 1);
                    $DataCount++;
                }
    
                $ControlBitPos++;
    
    
                if($ControlBitPos = 8)
                {
                    $ControlBitPos = 0;
                    $ControlPos++;
                }
                else
                {
                $Decompress = $DecompData;
                }
                $DataPos++;
            }
            echo $DecompData;
    }
    
    $FileName = 'test.bin';
    $FName = $FileName;
    
    $string56 = array_merge(range('0', '9') , range('a' , 'z'));
    $string56 = array_merge($string56 , range('A' , 'Z'));
    $zero = '0';
    while($zero < 6) { $full .= $string56[rand('0' , count($string56))]; $zero++; }
    
    $data = file_get_contents($FileName);
    $return = Decompress($data);
    
    $file = fopen($full . '.jpg','w');
    fwrite($file,$return);
    fclose($file);
    
    echo $return . '<br /><br />';
    echo $full . '.jpg';
    ?>
    
    Code (markup):
     
    Pudge1, Aug 20, 2009 IP
  8. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well, you'll want to do strlen on the string instead of count, for one.

    $zero = 0; instead of $zero = '0';

    On first sight.
     
    premiumscripts, Aug 20, 2009 IP
  9. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #9
    That's not it. I had that there before with no problems. It is with the new part I added at the top. function Mid()
     
    Pudge1, Aug 20, 2009 IP
  10. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Add:

    error_reporting(E_ALL);
    ini_set('display_errors', 1); 
    PHP:
    at the top of the file.. If it still doesn't show output, you'll have to look into the apache or php error logs.
     
    premiumscripts, Aug 20, 2009 IP
  11. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #11
    Okay I put it in then I got this:

    Notice: Undefined variable: full in /home/nintendo/public_html/brawl/brawl.php on line 82

    Notice: Undefined offset: 62 in /home/nintendo/public_html/brawl/brawl.php on line 82

    Notice: Undefined variable: return in /home/nintendo/public_html/brawl/brawl.php on line 14

    The last one repeats endlessly. I am not sure what the second one means but all of those variables are defined so I don't know what it is talking about.
     
    Pudge1, Aug 24, 2009 IP
  12. iamben

    iamben Active Member

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #12
    Sorry to jump in late - I know you can reference a character in a string like premiumscripts said; the function to convert a string to an array is 'str_split'..

    $str = "Hello Friend";
    $arr1 = str_split($str);
    print_r($arr1);

    Array
    (
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
    )
     
    iamben, Aug 24, 2009 IP
  13. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #13
    Thank you! I will try this out and post the results.
     
    Pudge1, Aug 24, 2009 IP
  14. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #14
    Hmm still getting a 500 error.

    
    <?
    function Mid($str,$start,$length)
    {
    $array = str_split($str);
    $number = $start;
    while($length > $number)
    {
    $return = $return . $array[$number];
    $number++;
    }
    echo $return;
    }
    
    function CLng($str)
    {
    $array = explode('.',$str);
    return $array[0];
    }
    
    function Decompress($DecompData)
    {
        if (strlen($DecompData) > 4)
        {
            $ControlCount = ord(substr($DecompData,0,1)) *16777216 + ord(Mid($DecompData, 2, 1)) *65536 + ord(Mid($DecompData, 3, 1)) *256 + ord(Mid($DecompData, 4, 1));
            $DataCount = ord(Mid($DecompData, 5, 1)) *16777216 + ord(Mid($DecompData, 6, 1)) *65536 + ord(Mid($DecompData, 7, 1)) *256 + ord(Mid($DecompData, 8, 1)) + 9;
            $Decompress = Mid($DecompData, $DataCount, 4);
            $DataCount = $DataCount + 4;
            $ControlBitPos = 0;
            $ControlPos = 9;
        }
        else
        {
        }
    
    
            $DataPost = 1;
    
            while($DataPost < $ControlCount)
            {
                if(pow(2, $ControlBitPos) == (ord(substr($DecompData, $ControlPos, 1))) && pow(2, $ControlBitPos))
                {
                    $DecompStart = strlen($Decompress) - (CLng(ord(Mid($DecompData, $DataCount, 1))) *256 + CLng(ord(Mid($DecompData, $DataCount + 1, 1)))) + 1;
                    $DecompLen = ord(Mid($DecompData, $DataCount + 2, 1));
                    $Decompress = $Decompress & Mid($Decompress, $DecompStart, $DecompLen);
                    $DataCount = $DataCount + 3;
                   }
                else
                {
                    $Decompress = $Decompress . Mid($DecompData, $DataCount, 1);
                    $DataCount++;
                }
    
                $ControlBitPos++;
    
    
                if($ControlBitPos = 8)
                {
                    $ControlBitPos = 0;
                    $ControlPos++;
                }
                else
                {
                $Decompress = $DecompData;
                }
                $DataPost++;
            }
            echo $DecompData;
    }
    
    $FileName = 'test.bin';
    $FName = $FileName;
    
    $string = array_merge(range('0', '9') , range('a' , 'z'));
    $string = array_merge($string , range('A' , 'Z'));
    $zero = '0';
    
    while($zero < 6)
    {
    $full = $full . $string[rand('0' , count($string))];
    $zero++;
    }
    
    $data = file_get_contents($FileName);
    $return = Decompress($data);
    
    $file = fopen($full . '.jpg','w');
    fwrite($file,$return);
    fclose($file);
    
    echo $return . '<br /><br />';
    echo $full . '.jpg';
    ?>
    
    Code (markup):
     
    Pudge1, Aug 24, 2009 IP
  15. iamben

    iamben Active Member

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #15
    Um, you might be best not using return as a variable name. Whilst I'm sure it's fine, it makes the code kinda confusing to read.

    Saying that, try setting $full = ''; and $return = ''; before you get into the while loop. That should get rid of the undefined variables. I'd hazard a guess the other error on line 62 is because you're trying to access a random array value in $string from 0 to count($string). Arrays are indexed from zero, and your count is the total count starting at 1. So the random number may be:
    0,1,2,3,4,5 (or whatever)
    and the array count is 6 (or whatever), so you're getting the error when the random value is hitting [6]..

    Any help?
     
    iamben, Aug 25, 2009 IP
  16. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #16
    I fixed all of those problems however I still get the 500 error. Thanks for trying to help though I really appreciate it.
     
    Pudge1, Aug 25, 2009 IP
  17. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #17
    That has nothing to do with this script.. I already told you to:

     
    premiumscripts, Aug 25, 2009 IP
  18. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #18
    Oh sorry must have missed that lol. I'll look now.
     
    Pudge1, Aug 25, 2009 IP