www.webdeveloper.com
Results 1 to 8 of 8

Thread: Converting plaintext back to array.

  1. #1
    Join Date
    Apr 2003
    Location
    Canyon Country, CA
    Posts
    734

    Converting plaintext back to array.

    I am currently printing an array (serialized and gzipped) to a page. I would like to convert the plaintext (uncompressed and unserialized) back into an array.

    The array is multi-dimensional. An example of the array is below:
    PHP Code:
    Array
    (
        [
    test2] => Array
            (
                [
    0] => Array
                    (
                        [
    name] => iPod.png
                        
    [filesize] => 47224
                        
    [filename] => iPod.png
                        
    [md5] => f048ce6d5dd38236384ce6fe4729743f
                        
    [series] => test2
                    
    )

                [
    1] => Array
                    (
                        [
    name] => song.txt
                        
    [filesize] => 696
                        
    [filename] => song.txt
                        
    [md5] => 1ad394e40df12e63432e0d24eff8c2f1
                        
    [series] => test2
                    
    )

                [
    2] => Array
                    (
                        [
    name] => winteryawn.jpg
                        
    [filesize] => 22977
                        
    [filename] => winteryawn.jpg
                        
    [md5] => a3c6840a188bfb9cfbc13c6854a42f61
                        
    [series] => test2
                    
    )

                [
    3] => Array
                    (
                        [
    name] => raidz.png
                        
    [filesize] => 7854
                        
    [filename] => raidz.png
                        
    [md5] => 02fd1c918491276c29969034970ddb98
                        
    [series] => test2
                    
    )

            )

        [
    test] => Array
            (
                [
    0] => Array
                    (
                        [
    name] => iPod.png
                        
    [filesize] => 47224
                        
    [filename] => iPod.png
                        
    [md5] => f048ce6d5dd38236384ce6fe4729743f
                        
    [series] => test
                    
    )

                [
    1] => Array
                    (
                        [
    name] => song.txt
                        
    [filesize] => 696
                        
    [filename] => song.txt
                        
    [md5] => 1ad394e40df12e63432e0d24eff8c2f1
                        
    [series] => test
                    
    )

                [
    2] => Array
                    (
                        [
    name] => files.php
                        
    [filesize] => 2731
                        
    [filename] => files.php
                        
    [md5] => 8a0619677a72695d014fe78d50a63163
                        
    [series] => test
                    
    )

                [
    3] => Array
                    (
                        [
    name] => winteryawn.jpg
                        
    [filesize] => 22977
                        
    [filename] => winteryawn.jpg
                        
    [md5] => a3c6840a188bfb9cfbc13c6854a42f61
                        
    [series] => test
                    
    )

                [
    4] => Array
                    (
                        [
    name] => raidz.png
                        
    [filesize] => 7854
                        
    [filename] => raidz.png
                        
    [md5] => 02fd1c918491276c29969034970ddb98
                        
    [series] => test
                    
    )

            )



  2. #2
    Join Date
    Jun 2006
    Posts
    384
    what's the plaintext look like? looks like xml would be a good format for it...

  3. #3
    Join Date
    Apr 2003
    Location
    Canyon Country, CA
    Posts
    734
    Quote Originally Posted by cridley
    what's the plaintext look like? looks like xml would be a good format for it...
    I posted the plaintext...:P

    Here it is again:

    PHP Code:
    Array 

        [
    test2] => Array 
            ( 
                [
    0] => Array 
                    ( 
                        [
    name] => iPod.png 
                        
    [filesize] => 47224 
                        
    [filename] => iPod.png 
                        
    [md5] => f048ce6d5dd38236384ce6fe4729743f 
                        
    [series] => test2 
                    


                [
    1] => Array 
                    ( 
                        [
    name] => song.txt 
                        
    [filesize] => 696 
                        
    [filename] => song.txt 
                        
    [md5] => 1ad394e40df12e63432e0d24eff8c2f1 
                        
    [series] => test2 
                    


                [
    2] => Array 
                    ( 
                        [
    name] => winteryawn.jpg 
                        
    [filesize] => 22977 
                        
    [filename] => winteryawn.jpg 
                        
    [md5] => a3c6840a188bfb9cfbc13c6854a42f61 
                        
    [series] => test2 
                    


                [
    3] => Array 
                    ( 
                        [
    name] => raidz.png 
                        
    [filesize] => 7854 
                        
    [filename] => raidz.png 
                        
    [md5] => 02fd1c918491276c29969034970ddb98 
                        
    [series] => test2 
                    


            ) 

        [
    test] => Array 
            ( 
                [
    0] => Array 
                    ( 
                        [
    name] => iPod.png 
                        
    [filesize] => 47224 
                        
    [filename] => iPod.png 
                        
    [md5] => f048ce6d5dd38236384ce6fe4729743f 
                        
    [series] => test 
                    


                [
    1] => Array 
                    ( 
                        [
    name] => song.txt 
                        
    [filesize] => 696 
                        
    [filename] => song.txt 
                        
    [md5] => 1ad394e40df12e63432e0d24eff8c2f1 
                        
    [series] => test 
                    


                [
    2] => Array 
                    ( 
                        [
    name] => files.php 
                        
    [filesize] => 2731 
                        
    [filename] => files.php 
                        
    [md5] => 8a0619677a72695d014fe78d50a63163 
                        
    [series] => test 
                    


                [
    3] => Array 
                    ( 
                        [
    name] => winteryawn.jpg 
                        
    [filesize] => 22977 
                        
    [filename] => winteryawn.jpg 
                        
    [md5] => a3c6840a188bfb9cfbc13c6854a42f61 
                        
    [series] => test 
                    


                [
    4] => Array 
                    ( 
                        [
    name] => raidz.png 
                        
    [filesize] => 7854 
                        
    [filename] => raidz.png 
                        
    [md5] => 02fd1c918491276c29969034970ddb98 
                        
    [series] => test 
                    


            ) 



  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    I suppose you could write a function to parse the text line by line, look for values in brackets, note indent level, etc., etc., and eventually build an array from the textual data; but, I would start by asking: why are you going through these evolutions?

    My guess is that by understanding your functional requirements that led you to this (rather strange to me) sequence of events, we might be able to suggest a simpler, cleaner, and easier solution to your needs.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Apr 2003
    Location
    Canyon Country, CA
    Posts
    734
    Well, I couldn't get the function to work (from the other thread I posted).

    So what I'm doing is a file_get_contents on the file. The file itself is serializing then gzdeflating the output. The file reading the contents inflates and unserializes.

    I was thinking of using explode then implode. Is there a way to just use serialize to get it back into an accessible array?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    I'm still confused as to what you are doing. If you're trying to save an array to a file and then read it back into an array at a later time, you should be able to simply do:
    PHP Code:
    // save array to file:
    $myArray = array([array definition here]);
    $fh fopen("somefile""w");
    fwrite($fhserialize($myArray));
    fclose($fh);

    // get array from file
    $serialized file_get_contents("somefile");
    if(
    $serialized !== FALSE)
    {
      
    $myArrayCopy unserialize($serialized);
    }
    else
    {
      
    // handle error here

    So what is it I'm not understanding about what you want to do?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    If you used var_export rather than print_r to produce the plaintext file the array would already be in a usable form.

  8. #8
    Join Date
    Apr 2003
    Location
    Canyon Country, CA
    Posts
    734
    NogDog, the problem is I'm not saving it to a file. However it looks like the second half of what you posted will work.

    Bokeh, I want to serialize the array. I can't serialize it using var_export =(

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles