#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    58
    Rep Power
    25

    Parsing through text


    I haven't touched PHP in a long time and needed some help.
    I have survey and in the survey once you upload the documents, it stores them as such in the database:

    [{ },{ "size":"126.189"size":"126.469","name":"RFC_MarketingReqForm.docx","filename":"fu_378r8fczc3u5nen"," ext":"docx"","name":"RFC_ARMCBillpay_081412.docx","filename":"fu_ws6t2urxz2qg39c","ext":"docx" },{ "size":"126.469","name":"RFC_MarketingReqForm.docx","filename":"fu_7ahdzpscp5dh82w","ext":"docx" }]

    I need to go through this and pull out the filename and ext to create a url link to the documents on a custom reports page that I am creating.

    PHP Code:
    $text $row['response'];
    $newtext preg_split"/ (;|\"|{|}|,) /"$text ); 
    $newtext explode(','$text); 
    $count count($newtext); 
    for (
    $i 0$i $count$i++) {  
    echo 
    $newtext[$i] . "<br />"

    I was trying to break it apart piece by piece, but I realized I am not really sure how to get the filename and ext out alone. Thank you.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    whoever designed this database, slap him and don't ever let him near the code again -- I hope it wasn't you?

    Sh*t like this is exactly why people preach normalization, but obviously some "developers" still have never heard of it.

    OK, so you have a bunch of crap data which looks like JSON. So what you'll have to do is take a JSON parser (if my guess was right) and read out the pieces you want to fetch -- in the hopes that the string isn't broken.

    If you have any chance to repair the database, that's actually the first thing you should do.

    Do not fumble with home-made regexes. That just makes everything even crappier.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    58
    Rep Power
    25
    It is from LimeSurvey, open source survey system.

    I kept on reading and came across the same thing that it is JSON. I am trying to get it to work with json_decode, will update my progress if I succeed.
  6. #4
  7. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    the data you posted is corrupt. If that is a direct copy/paste from the system then you are in trouble.

    I fixed the data up (I had to make up some stuff to fill in the blanks and fix the structure). I ended up with:

    Code:
    [{ },
    { "size":"126.469","name":"RFC_MarketingReqForm.docx","filename":"fu_378r8fczc3u5nen","ext":"docx" },
    { "size":"126.469","name":"RFC_ARMCBillpay_081412.docx","filename":"fu_ws6t2urxz2qg39c","ext":"docx" }, 
    { "size":"126.469","name":"RFC_MarketingReqForm.docx","filename":"fu_7ahdzpscp5dh82w","ext":"docx" }]
    that let me call json_decode() on it, which output this array:

    Code:
    Array
    (
        [0] => Array
            (
            )
    
        [1] => Array
            (
                [size] => 126.469
                [name] => RFC_MarketingReqForm.docx
                [filename] => fu_378r8fczc3u5nen
                [ext] => docx
            )
    
        [2] => Array
            (
                [size] => 126.469
                [name] => RFC_ARMCBillpay_081412.docx
                [filename] => fu_ws6t2urxz2qg39c
                [ext] => docx
            )
    
        [3] => Array
            (
                [size] => 126.469
                [name] => RFC_MarketingReqForm.docx
                [filename] => fu_7ahdzpscp5dh82w
                [ext] => docx
            )
    
    )
    so from that you could get the filename using a loop and:

    PHP Code:
    $filename $array[$i]['filename'].".".$array[$i]['ext']; 
    also bare in mind that json_decode($jsondata,true) will give you an assoc array which you can reference like i did above, while omitting the true will give you an object.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?

IMN logo majestic logo threadwatch logo seochat tools logo